How to continue script after recieveing information

Hi, so basically I want to make a script that only continues if the server gives info.

Example:
(bad scripting, just to show my point)

send a remote event to server with something

only continue script if the server with a remote event gives info back

continue script

I’ve heard you can use remote functions to yield but I am not sure.

Use a remote function:

local remoteFunction = path.to.remote.function
local result = remoteFunction:InvokeServer(...)
print(result)

Remote functions yield (pause) the thread until a result is returned from the server. The server-side code looks like:

local remoteFunction = path.to.remote.function
function remoteFunction.OnServerInvoke(player, ...)
    return 123
end
1 Like

Either you can use a remote function (as suggested above, which is also what I personally recommend) or if you really want to use remote events then you can do the following:

Server
local RemoteEvent = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"))

RemoteEvent.OnServerEvent:Connect(function(Player, WhateverYouSent)
	--Do stuff with whatever you sent.
	
	RemoteEvent:FireClient(Player, "Something else")
end)
Client
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

RemoteEvent:FireServer("Some Info")

RemoteEvent.OnClientEvent:Wait() --Will wait until server responds.

print("Done!")
2 Likes

How could I get the info the server is sending back?

And, lets say I have the FireAllClients() part in an if statement, and then the clients send info to another script and that script sends back a FireServer() thing and it waits for the server to respond, before continuing.

Changed it to save the information it gets from the server.

Server
local RemoteEvent = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"))

RemoteEvent.OnServerEvent:Connect(function(Player, WhateverYouSent)
	--Do stuff with whatever you sent.
	
	RemoteEvent:FireClient(Player, "Something else")
end)
Client
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

RemoteEvent:FireServer("Some Info")

local Result = RemoteEvent.OnClientEvent:Wait() --Will wait until server responds.

print(Result)
1 Like

Nevermind, I figured it out. Thanks for the help @ZtarZManZ and @Dandystan