Remote Events
Remote events send data to the specified place without checking if the other place received it. This means that it can send anything between the server-to-client and client-to-server. You can imagine this like sending someone a notification of an event, you send it to inform them, however you don’t expect a response.
--Script
local remoteEvent = game:GetService('ReplicatedStorage').RemoteEvent
remoteEvent:FireAllClients('Hello')
print('Hello World')
This when on the server would send the string “Hello” on every every client, and the script would continue right on and print “Hello World” even if it’s the only script in the game. The server should always use remote events. Remote Events also use a :connect() to specify a response.
--Local Script
local remoteEvent = game:GetService('ReplicatedStorage').RemoteEvent
remoteEvent.OnClientEvent:Connect(function(data)
print(data) --Prints Hello
end)
Keep in mind you can also send data to the server from the client from using :FireServer() rather than :FireClient(), however you can not FireClient from the Client or FireServer from the Server.
Remote Functions
A remote function treats communication a little differently. Remote functions wait for a response, meaning that it should only be used for client-to-server communication. Think of it as an RSVP, you send it out and wait for their response. (However imagine in this case you don’t host the event unless they respond.)
--Script
local remoteFunction = game:GetService('ReplicatedStorage').RemoteFunction
response = remoteFunction:InvokeClient(game.Players.Player1, 'Hello')
print('Hello World')
This is where it gets a little bit different than a remote event. If you were just to throw this into a script in ServerScriptService and ran it without a client response, it wouldn’t print “Hello World”! Why? Well because RemoteFunctions hang (or more known as wait), for a response from the client. Responses for remote functions are also a little bit different, with it being a function you set to the OnClientInvoke property of RemoteFunctions. To send data from the invokee to the invoker you must simply return something from the function.
--Local Script on Player1
local remoteFunction = game:GetService('ReplicatedStorage').RemoteFunction
local function respond(data)
return data + ' to you too!' --Would send the server "Hello to you too!"
end
With this added, the server will now print “Hello World” as it got a response. The response variable from the script would also now contain the string “Hello to you too!”. The example here should not be used in production. The server should in practice never invoke the client as if an exploiter disables the script for the invoke or an error happens that breaks the local script, the script on the server will hang forever which is bound to cause more logic breaks.
Client-to-Client Communication
You also seem interested in client-to-client communication. If you mean client communication as in the same client sending data to another script, then you can simply use bindable functions/events which is a little different than remotes. However if you are talking communication from one client to a different client, you would have to have the server act as a middle man.
--Local script
remoteEvent = game:GetService('ReplicatedStorage'):FindFirstChild("RemoteEvent")
function clientToClient(clientName, data)
remoteEvent:FireServer(clientName, data)
end
function clientResponse(clientName, data)
return clientName + ' sent ' + data
end
clientToClient(Player2, 'Hi') --Imagine this was only called from Player1's client.
print(remoteEvent.OnClientEvent:Connect(clientResponse)) --On Player2 this would print 'Player1 sent Hi'
This would be the client side, and the server side would look like this.
--Server
remoteEvent = game:GetService('ReplicatedStorage'):FindFirstChild("RemoteEvent")
remoteEvent.OnServerEvent:Connect(function(player, sendPlayer, data) --Ideally you'd want some input validation here.
remoteEvent:FireClient(sendPlayer, player, data)
end)
This would allow the clients to interact each other with the server acting as the middle man to move messages. Obviously you want some validation the clients should be able to message each other, the data sent is correct, etcetera. Hopefully this helped you understand!