I'm kinda struggling with using remote function I understand remote events but can someone explain remote functions to me?
What do remote functions do? I don't understand.
1 answer
RemoteFunctions server same purpose at remote events, communication between server and client.
Remote functions have 2 features that remote events do not have and those are returning values and yielding, they have the same syntax but a bit different method names. InvokeServer is the mostly used one and is called from the client.
-- LocalScript (StarterPlayerScripts)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- You must create one in the explorer or in server script
local RemoteFunction = ReplicatedStorage.RemoteFunction
RemoteFunction:InvokeServer("Zefifixutre")
print("Invoked")
This will invoke the server, however, just like with remote events you need to put callback onto the function, this case will yield forever as i did not bind any callback on the server to the remote function.
Unlike remote events, remote functions can only have one callback function connected to them, to bind it you need to assign any function to the OnServerInvoke property of the remote function (OnClientEvent if in LocalScript).
-- Script (ServerScriptService)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Same remote function from the LocalScript
local RemoteFunction = ReplicatedStorage.RemoteFunction
local function OnInvoked(Client, Name)
print(Name) -- Zefifixutre
end
RemoteFunction.OnServerInvoke = OnInvoked
Just like with remote events, first parameter that the invoke passes is the invoking client. Now if you run the simulation with both of the scripts you should see following output
Zefifixutre - Server - Script:8
Invoked - Client - LocalScript:8
In this case however there is almost no point in using remote function instead of remote event, difference between them in this case would be that remote function would yield until the OnInvoked function finished. If you would change the syntax to fit with remote events and keep the functions, you would get the following output:
Invoked - Client - LocalScript:8
Zefifixutre - Server - Script:8
Inverted because the print in local script is right after the invoke so before the server catches the signal, it can print it at least 10 thousand times.
At the beginning i was writing about the features of the remote functions, they can yield the script that invoked them and can also return values, here are some examples
-- LocalScript (StarterPlayerScripts)
local RemoteFunction = ReplicatedStorage.RemoteFunction
local InvokeTime = os.clock()
local Animal, Age = RemoteFunction:InvokeServer()
print("Got result in:", os.clock() - InvokeTime, "seconds")
print("Result:", Animal, Age)
-- Script (ServerScriptService)
local RemoteFunction = ReplicatedStorage.RemoteFunction
local function OnInvoke()
wait(5)
return "cat", 123
end
RemoteFunction.OnServerInvoke = OnInvoke
Get result in: 5.0012 seconds - Client - LocalScript:4
Result: cat 123 - Client - LocalScript:4
I also wrote that invoking client is bad, the reason for that is because when you invoke a client, if the client leaves your server script invoking him will error, else if the invoked client does not return anything the server script will yield forever, there is also no point in invoking client anyway, remote events are better at this.
If you want to invoke the client, it's best to use pcall when doing that, it will catch the error if it happens.
You should read this, it explains how remote functions work.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Remote Functions are very similar to remote events. Except when a remote function is finishing executing a script on one end it will send a 'response' back to the script the function was initially called in.