Skip to main content

Network

These network APIs allow you to send data to players and receive data from players.

Global Methods

Net.SendToTarget(player: Player, event: string, data: any)

Triggers a corresponding client event for a specific player in any resource with the same event name. data is not required. Any type of data except functions is valid, including strings, numbers, tables, metatables, etc.

Net.SendToTargets(players: Player[], event: string, data: any)

Triggers a corresponding client event for a table of players.

Net.Send(event: string, data: any)

Triggers a corresponding client event for all players.

Example usage:

-- resources/test/server/main.lua

Net.Send("BroadcastEvent", { value = "hello all!"})

A corresponding client script in any resource can listen for this event:

-- resources/test/client/main.lua

Net.AddEvent("BroadcastEvent", function(args)
print("Received: " .. tostring(args.value))
end)

Net.AddEvent(event: string, handler: function)

Adds an event handler for a specific network event with a string name.

Example (on the server):

-- resources/test/server/main.lua

Net.AddEvent("TestEventFromClient", function(args)
print("Received: " .. tostring(args.value))
end)

And the client can send events to trigger the client event:

-- resources/test/client/main.lua

Net.Send("TestEventFromClient", { value = "hello"})

Another example: To get the player who sent the event, you can use Net.Sender():

-- resources/test/client/main.lua
Net.Send("PrintMySkin")
-- resources/test/server/main.lua

-- Get the PlayerClient that sent the event, get NetPlayer from it, and print their skin
Net.AddEvent("PrintMySkin", function()
local player = Net.Sender():GetNetPlayer()
print("Player skin: " .. tostring(player:GetSkin()))
end)

Net.Sender(): PlayerClient

Returns the PlayerClient that sent the event. This is only valid within the event handler.

Example:

-- resources/test/server/main.lua

Net.AddEvent("PrintMyNick", function()
local player = Net.Sender()
print("Player: " .. tostring(player:GetNick()) .. " sent the event")
end)

Net.RemoveEvent(event: string)

Removes a network event handler.

Net.Fetch(url: string, args?: table): table

Performs HTTP requests similar to JavaScript's fetch API. This is a synchronous operation that returns a table with the response data.

Parameters:

  • url (string): The URL to make the request to
  • args (table, optional): Options table for configuring the request

Args Table Properties:

  • method (string): HTTP method (defaults to "GET" if no args provided, "POST" if args provided)
  • body (string): Request body content
  • content_type or contentType (string): Content-Type header
  • authorization (string): Authorization header
  • accept (string): Accept header
  • headers (table): Table of custom headers as key-value pairs
  • timeout (number): The maximum amount of time in seconds to wait for a response. Defaults to 10 seconds.

Return Table Properties:

  • response (string): The response body as a string
  • status (number): HTTP status code (e.g., 200, 404, 500)
  • success (boolean): Whether the request was successful

Examples:

Simple GET request:

-- resources/test/server/main.lua

local result = Net.Fetch("https://api.example.com/data")
if result.success then
print("Status:", result.status)
print("Response:", result.response)
else
print("Request failed with status:", result.status)
end

POST request with JSON data:

-- resources/test/server/main.lua

local result = Net.Fetch("https://api.example.com/submit", {
method = "POST",
body = '{"username": "player123", "score": 1500}',
contentType = "application/json"
})

if result.success and result.status == 200 then
print("Data submitted successfully:", result.response)
else
print("Failed to submit data. Status:", result.status)
end

Request with custom headers:

-- resources/test/server/main.lua

local result = Net.Fetch("https://api.example.com/protected", {
method = "GET",
headers = {
["X-API-Key"] = "your-api-key-here",
["User-Agent"] = "JC4MP-Server/1.0"
}
})

if result.success then
print("Protected data retrieved:", result.response)
end

Form data submission:

-- resources/test/server/main.lua

local result = Net.Fetch("https://api.example.com/form", {
method = "POST",
body = "name=John&[email protected]",
contentType = "application/x-www-form-urlencoded"
})

if result.success then
print("Form submitted:", result.response)
end

Handling different response types:

-- resources/test/server/main.lua

local result = Net.Fetch("https://api.example.com/status")

if result.success then
if result.status == 200 then
print("Success:", result.response)
elseif result.status == 404 then
print("Resource not found")
elseif result.status >= 500 then
print("Server error:", result.status)
end
else
print("Network error or request failed")
end