How to use Remote events and Remote func

This tutorial talks about Remote functions and events and the bindable versions.

by MadnessMyth

Author Avatar

Remote events


Remote events can be used to send data from a local script to a server script. This can be the other way around. Some data can not be sent such as

Examples of sending data and getting data.

Getting data from a local script to the server script.

RemoteEvent.OnServerEvent:Connect(function(Player, Data1, Data2, Data3, ...)
	--Local script to Server script
end)

This example uses OnServerEvent which is a RBXScriptSingal, you can connect functions to this signal and disconnect it whenever. This is used for getting data from the local script to the server script. And data can be sent out.

As for getting data from the server, its quite similar.

Getting data from a Server script to a local script.

RemoteEvent.OnClientEvent:Connect(function(Data1, Data2, Data3, ...)
	--Server script to local script
end)

Sending data from local or server scripts.

This gets data sent from the server script to the local script. Now we need to learn how to send data from the local or server script. For a local script its as easy as doing:

RemoteEvent:FireServer(Data1, Data2, Data3)

FireServer is an Event only used by the Local script. While FireClient needs a player as its first input.

RemoteEvent:FireServer(PlayerInstance, Data1, Data2, Data3)

If you don't want to send data to only a player. You can do this instead.

RemoteEvent:FireAllClients

RemoteFunctions


Remote functions are like Remote Events, but when you send data, it returns data.

Sending data to server and server responding.

To send a message to the server script we can use:

--Local script
local Result = RemoteFunction:InvokeServer(1, 1)
print(Result)

This sends a message to the server, Now the server has the message: 1, 1 We want to add 1, 1 for this example.

But There is no .OnServerEvent, instead its:

--Server script
RemoteFunction.OnServerInvoke = function(Player, Data1, Data2)
	return Data1 + Data2
end

OnServerInvoke is a function, we can change it. This function gets ran when a client sends a message to the server. But with functions we can return data.

In this example, The client tells the server 1, 1 and waits for a response. We changed OnServerInvoke to add 1 and 1 together and gives it back.

The local script prints this result.

2

We have just made a RemoteFunction.

Now lets flip it the other way around.

Sending data from server to client.

--Server script
local Result = RemoteFunction:InvokeClient(Player, 1, 1)
print(Result)

This sends a message to "Player". But we can't send it to all clients as we can't get results for each one so it doesn't exist. You can use a loop

for i,v in ipairs(game.Players:GetPlayers()) do

end

But what about handling the message on the client?

RemoteFunction.OnClientInvoke = function(Data1, Data2)
	return Data1 + Data2
end

Its basically the same but without the Player variable.

-Security notice!

When coding a OnServerInvoke do not trust Local scripts. Exploiters can change the code of your game and make it malicous. Do checks on the Server script, such as for a purhase, check if the buyer has enough Money before purchasing. And when using OnClientInvoke heavily do not trust the client. As this means the server asks for data from the client. The client can send anything back.

Extra notices


If you place a RemoteEvent or RemoteFunction inside ReplicatedFirst, they wont work.


However if your using a Local script, You can place a BindableEvent or BindableFunction inside of ReplicatedFirst and they will work.

Now you know how to use Remote events and remote functions. But what about Bindable events?

BindableEvents


Bindable events send data over server script to server script. Or local script to local script.

BindableEvents have a RBXScriptSignal just like RemoteEvents.

##Sending data over scripts

BindableEvent.Event:Connect(function(Data1, Data2)

end)

.Event is like OnServerEvent. And we can run .Event by doing

BindableEvent:Fire(Data1, Data2)

##Script execution failure

Do not fire .Event inside of .Event on it self. As it will keep executing it self and resulting in a error. Instead use Bindable functions

##Bindable functions

Bindable functions are like RemoteFunctions but there is no player input.

##Getting data and sending data over scripts.

We can get data using

RemoteFunction.OnInvoke = function(Data1, Data2)
	return Data1+Data2
end

OnInvoke is a function that is executed when we invoke RemoteFunction. We can send data and get data using

local result = RemoteFunction:Invoke(Data1, Data2)
print(result)

This code is basically the same as the last one, It adds Data1 and Data2 (Assuming its a number). And it will add them. And print it. Data1 being 1 and Data2 being 1, will result in

2

##Script execution failure

Do not fire .OnInvoke inside of an .OnInvoke on it self. As it will keep executing it self and resulting in a error.

End of tutorial

View in-game to comment, award, and more!