RemoteFunction
The RemoteFunction
class in represents a remote function that can be invoked and heard on the client and server side. It enables communication between server and client scripts by allowing remote calls to be made and handled with params that you define yourself.
-- Client and Server script:
local remoteFunction = RemoteFunction.new("TestRemoteFunction");
local lastSendTime = 0;
local lastPlayer = nil;
function self:ClientStart()
-- Get the tap handler on ourselves and invoke server on the remote function when tapped with a custom callback message
self.gameObject:GetComponent(TapHandler).Tapped:Connect(function()
print ("Tapped")
remoteFunction:InvokeServer("Hello from client!", function(response)
print(response)
end)
end)
-- when the client is invoked, print the server's message
remoteFunction.OnInvokeClient = function(message)
print (message)
return true;
end
end
function self:ServerStart()
server.PlayerConnected:Connect(function(player)
lastPlayer = player
end)
remoteFunction.OnInvokeServer = function(message)
print (message)
return "Hello from Server!";
end
end
function self:ServerUpdate()
lastSendTime = lastSendTime + Time.deltaTime;
if lastSendTime > 2 then
remoteFunction:InvokeClient(lastPlayer, "Unsolicited message from server!", function(response)
print(tostring(response))
end)
lastSendTime = 0
end
end
Properties
The name of the remote function.
Callback function for the client that is triggered by the server. Parameters are determined by what was passed into InvokeClient
.
Callback function for the server that is triggered by the client. Parameters are determined by what was passed into InvokeServer
.
Methods
Invokes the remote function on the client side.
Parameters
player
PlayerThe player to invoke the function for.