A better way to understand Remote Events and Remote Functions

So I guess many of you who is beginners are having confusion from Remote Events and Remote Functions. This guide will help you to understand it better.

So if you understand/know about functions in roblox then think remote events and remote functions like this

It is basically doing a function but on a different script (Server if you fire from client) and so on.

For example:

local function getRandomNumber()
   local a = math.random(1, 10) -- get random number from 1 and 10
   print(a)
   return a -- return the value
end

getRandomNumber()

Now if you put this on server, it will print on the server, on client then it will be on client.
Right?

So you get it, you triggered the function so it executed

Now remember :FireServer() on Remote Event and :InvokeServer() on Remote Functions?

It’s just like triggering a function
Now I will use the same script using remote functions and remote events

-- remote event part --
-- local script
remoteEvent:FireServer()
-- server script
local function getRandomNumber()
   local a = math.random(1, 10) -- get random number from 1 and 10
   print(a)
   return a -- return the value
end

remoteEvent.OnServerEvent:Connect(function(getRandomNumber)
-- remote function part --
-- local script
remoteFunction:InvokeServer()
-- server script
local function getRandomNumber()
   local a = math.random(1, 10) -- get random number from 1 and 10
   print(a)
   return a -- return the value
end

remoteFunction.OnServerInvoke = getRandomNumber

So yeah you see it’s just doing functions on different scripts (server if local script, client if server script).

So what’s is the different from remote functions and remote events?

Remote functions will do all the code before continue the code on the script that triggers it

For example

-- remote function part --
-- local script
remoteFunction:InvokeServer()
print("Hi")
-- server script
local function getRandomNumber()
   local a = math.random(1, 10) -- get random number from 1 and 10
   print(a)
   wait(3) -- 
   return a -- return the value
end

remoteFunction.OnServerInvoke = getRandomNumber

What would this do is after 3 seconds, it will print “hi” on client.
Try it.

But if you do this with a remote event, it will print “hi” immediately.

So all I was doing was about a client fire to server
Now what if server fire to client?

Remember: The first default argument will always be the player when server fire to client
For example

-- server script
remoteEvent:FireClient(player)
-- local script
remoteEvent.OnClientEvent:Connect(function(player)

What if I don’t get the first argument as the player?

It will print an error since it doesn’t know what is the client it will fire to.
Note: FireAllClients() doesn’t require a player argument but on client, the first argument will always be the player

For example:

-- server script
remoteEvent:FireAllClients()
-- local script
remoteEvent.OnClientEvent:Connect(function(player)

Now onto remote functions
The great thing about remote function is you can return a value from a function

You remember that you can return value in function?

local function getRandomNumber()
   local a = math.random(1, 10) -- get random number from 1 and 10
   return a -- return the value
end

Sadly, remote events can’t do function with a returned value but remote functions can
What if you try to return a value using remote events?

-- local script
local randomValue = remoteEvent:FireServer()
print(randomValue) -- nil
-- server script
local function getRandomNumber()
   local a = math.random(1, 10) -- get random number from 1 and 10
   return a -- return the value
end

remoteEvent.OnServerEvent:Connect(function()
  local randomNumber = getRandomNumber()
  return randomNumber
end)

The randomValue will always be nil if you are using remoteEvent
but with remote functions it is possible:

-- remote function part --
-- local script
local randomValue = remoteFunction:InvokeServer()
print(randomValue)
-- server script
local function getRandomNumber()
   local a = math.random(1, 10) -- get random number from 1 and 10
   return a -- return the value
end

remoteFunction.OnServerInvoke = function()
    local randomNumber = getRandomNumber()
    return randomNumber
end)

Now it will print a random number between 1 and 10, randomValue is no longer nil.

So I hope this help you more in understand remote events and remote functions. Thanks you for reading!

61 Likes

Good tutorial.


Not in the way you do with remote functions, but the value can instead be passed as an argument and accessed via Remote.OnClientEvent:Connect(function(args, …) if needed.

Adding some sort of debounce to RemoteEvent.OnServerEvent (individual for every player) would also prevent exploiters from spamming them.

5 Likes

Also you should make sure that your remotes are secure. For example if you have a remote like “addmoney” or “givetool” replace them now with ones like “buycoins” “sellitems” “buyitems” “sellcoins” etc. The client can lie about any data it sends to the server and fire any remote at any time so you should always validate everything on the server! :wink:

6 Likes