- sendPlayerCount.lua README
The sendPlayerCount.lua script is designed for Multi Theft Auto (MTA) servers to periodically send server information, including the server name, player count, IP address, and online status, to a specified API endpoint. The script uses HTTP POST requests to transmit data in JSON format.
- Data Collection: Collects the server name, current player count, server IP, and a static "Online" status.
- Data Transmission: Sends the collected data as a JSON payload to the API endpoint
https://example.app/api/app. - Periodic Execution: Uses a timer to execute the data-sending function every 60 seconds.
- Callback Handling: Includes a placeholder callback function to handle the API response or errors.
- Multi Theft Auto (MTA): The script is designed to run on an MTA server.
- Lua Environment: Requires MTA's Lua functions (
getServerName,getPlayerCount,getServerConfigSetting,toJSON,fetchRemote,setTimer). - Network Access: The server must have internet access to send HTTP POST requests to the specified API endpoint.
-
Place the
sendPlayerCount.luafile in your MTA server's resource directory (e.g.,mods/deathmatch/resources/[your_resource]/). -
Add the script to your resource's
meta.xmlfile:<script src="sendPlayerCount.lua" type="server" />
-
Start or restart the resource using the MTA server console (
/start [resource_name]or/refresh).
- The script automatically starts sending data to the API upon resource start.
- Data is sent every 60 seconds (60000 milliseconds) in an infinite loop (
setTimerwith0iterations). - The API endpoint (
http://localhost/api/app) must be accessible and configured to accept POST requests with JSON data.
- Function
enviarDadosServidor:- Collects server data (name, player count, IP, and status).
- Converts data to JSON format using
toJSON. - Removes the JSON array brackets (
string.sub(tbl, 2, -2)) to match the expected API format. - Sends the data via
fetchRemotewithContent-Type: application/json.
- Function
callBack:- A placeholder function to handle the API response or errors (currently empty).
- Timer: Executes
enviarDadosServidorevery 60 seconds and runs immediately on resource start.
- API Endpoint: Modify the
urlvariable to point to a different API endpoint if needed. - Timer Interval: Adjust the
60000insetTimerto change the frequency of data transmission (in milliseconds). - Data Fields: Add or modify fields in the JSON table within
enviarDadosServidorto suit your API requirements. - Callback Logic: Implement logic in the
callBackfunction to handle API responses or errors (e.g., logging, error handling).
- Ensure the API endpoint supports the JSON format sent by the script (without array brackets).
- The script assumes the
fetchRemotefunction is available in the MTA environment. - Test the script in a development environment to verify API compatibility and handle potential network issues.
- API Errors: Check the
callBackfunction for error codes or messages returned by the API. - Network Issues: Verify that the MTA server can reach the API endpoint (
http://localhost/api/app). - JSON Format: Ensure the API accepts the JSON structure sent by the script.