Remote functions

I just finished watching an introduction to remote events and functions from TheDevKing (Advanced Roblox Scripting Tutorial #8 - Remote Events & Remote Functions (Beginner to Pro 2019) - YouTube) and I understand remote events pretty well, however, remote functions don’t really make sense as he showed me that they do the same thing.

Can anyone please explain the difference between remote events and remote functions and what remote functions actually do, it would be appreciated.

1 Like

4 Likes

RemoteFunctions are like Server > Client > Server

I’ve already read the wiki page.

I just honestly don’t see a use case. When would you ever need to use that?

Lets say as in that example ROBLOX used on the wiki. A player fires a bullet from the Client to the Server well RemoteEvents are only 1 way Events meaning you can only go from Client > Server or Server > Client. Now lets say you fired a bullet but using a RemoteFunction, a RemoteFunction is a 2 way communication meaning you fire the bullet from the Client to the Server and then BACK to the Client to make a check that it actually passed through the server safely then giving the player the point. I hope maybe this helps if not I will give an example!

RemoteEvents & RemoteFunctions are similiar, Both can be used For Server → Client or Client → Server Communication. The only difference is that RemoteFunctions can return values, Meanwhile RemoteEvents can’t.

For example, Let’s say that you have a Shop in your game, Only players with the Level above 10 can buy a certain item, You would need to check the Player’s level on the Server, As doing it on Client wouldn’t work at all. (Exploits, And can easily be bypassed)

You can use RemoteFunctions for that, Since they can return values and you can check them. Here’s what a simple example of them would look like (Assuming there’s a RemoteFunction called “GetLevel” on ReplicatedStorage):

-- Server Script

-- ReplicatedStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetLevel = ReplicatedStorage:WaitForChild("GetLevel")

-- Main

-- This is gonna fire whenever a Player invokes the Server
-- Let's say when they invoke the RemoteFunction (Client → Server)
-- They're requesting the value.

GetLevel.OnServerInvoke = function(Player)
	-- Assuming the Player has a Leaderboard folder...
	return Player.Leaderboard.Level.Value
end
-- Client Script

-- ReplicatedStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetLevel = ReplicatedStorage:WaitForChild("GetLevel")

-- Testing
-- We're gonna request the Server to gives us our Level.
local PlayerLevel = GetLevel:InvokeServer()

if PlayerLevel >= 10 then
	-- Code
end
4 Likes

Oh that makes sense now. Thank you!

1 Like

Last thing I’d add is that “:InvokeClient()” should be wrapped in a pcall() as it’s prone to errors (if the invoked client leaves/disconnects).

1 Like