Getting data back and forth

Hello!, I am making a game and wondering for a part of my game i need to get data from the server and bring it to the client so like a remote event but gets data then comes back

You need to use a RemoteFunction for this kind of communication. You can read more about it from that hyperlink I’ve included, but here’s a brief summary of it:

Remote Functions

  • Provides Player → Server → Player and Server → Player → Server communication
  • Called with InvokeServer/ InvokeClient
  • Event set with OnServerInvoke/ OnClientInvoke

Helpful tips

  • Try not to use OnClientInvoke/ InvokeClient
  • Use them whenever you need to gather information, i.e. Player stats
  • Double check everything on the server once recieved in case it’s been hooked

You shouldn’t use a remote function for server → client → server because a remote function yields the script until it gets a result. If it isn’t returned due to disconnects, lag, or exploiters, the whole server script will not continue.

1 Like

You are better off using a remote event from server to client, then send a remote back from client to server. You can send your data with the remote, and then do whatever and send the data back with the same remote.

how would I achieve that? I’m new to this topic

I am writing out a script, but do you need to send it to a specific client, or all of them?

Something like this:

Server:

--send the remote:
remote = game:GetService(“Replicated Storage”). --Add the name of your remote event here

data = put whatever data you want here

remote:FireAllClients(data)

--receive the remote back:
local function onRemoteReturn(player, data2)
    --put what ever code here, you can now reference the returned data as the variable ‘data2’, and the player who returned that data as ‘player’
end 

remote.OnServerEvent:Connect(onRemoteReturn)
    

Client Code:

remote = game:GetService(“Replicated Storage”). --the same name of the same remote event

local function onRemoteReceive(data)
   -- Do whatever data operations here, in the end declare your final result as a variable ‘data2’

    remote:FireServer(data2)
end

remote.OnClientEvent:Connect(onRemoteReceive)

This code is written to fire the remote event to all clients. To send the remote event to only one client, replace remote:FireAllClients(data) with remote:FireClient(game:GetService(“Players”).--specific player you want to send it to, data)