What They Are
- Remote Functions are a way for the client and server to communicate and get a return value.
- Unlike Remote Events (which are one-way), Remote Functions wait for a response.
Key Points
- Only one response is sent per call.
- Can be used to request data from the server or tell the server to return a value.
- They must be stored in ReplicatedStorage or another location accessible to both client and server.
Creating a Remote Function
- Go to ReplicatedStorage.
- Insert → RemoteFunction.
- Give it a name, e.g.,
GetPlayerData.
Server Script Example
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetPlayerData = ReplicatedStorage:WaitForChild("GetPlayerData")
-- Function to handle requests from the client
GetPlayerData.OnServerInvoke = function(player)
return "Hello, " .. player.Name
end
Client Script Example
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetPlayerData = ReplicatedStorage:WaitForChild("GetPlayerData")
-- Call the remote function
local result = GetPlayerData:InvokeServer()
print(result) -- Output: Hello, PlayerName
Important Notes