I have a main server script that runs a remote event, and i’d like to pause the script until the remote event is done running, how can I do this?
RemoteFunction.
However, this can be bad in practice because you can run into situations where the script that calls the InvokeClient method will never resume. A better approach would be to implement some sort of timer where if the server does not recieve a result within say 10 seconds, then it errors or catches this error and continues accordingly
What are u trying to do it will help us alot
Or just use a variable. Set it to true before u fire it and set it to false on the server script or smthg idk
local enabled = true
if enabled then
enabled = false
— what you want
wait(5) — record how long it takes to fire then set it to that time + 1 second
enabled = true
end
it’s simple!
local remote = game.ReplicatedStorage.RemoteEvent
remote.OnServerEvent:Wait()
--whatever you want to do
this script waits until the remote event is fired.
local remoteevent = game.ReplicatedStorage.RemoteEvent
local running = script.Parent.Value
remoteevent:FireServer(running)
repeat wait(1) until running == false
Like @East98 said, you should be using a RemoteFunction if you need your script to yield until the event is finished, not a RemoteEvent.
Also make sure to carefully consider what you are doing. Is it possible to re-create whatever you’re trying to do without having the server invoke the client? If so, use that instead as invoking the client can be troublesome for the reasons described in @East98’s post.
Thanks, this helped. I just fired a remote event at the end of the current remote event and waited until the final one fired.
Okay, i’m not too familiar with RemoteFunctions but i’ll look into it tomorrow. What i’m trying to do is a dialogue script. So from the server, a string is sent to everyones client and then I want it to wait until the dialogue is finished before sending the next dialogue and etc…
why dont u do it all in one event?
What @Pure_Bacn said.
Just send all the dialogue needed in a single event (in a table), then handle the dialogue on the client using a LocalScript
Oh yeah, you’re right. I’m so used to using different RemoteEvents for different tasks… Fixed it.
Good Job fixing it!!!
Remote functions can’t InvokeAllClients that’s why I can’t use them, I simply want to display some sort of GUI on all clients and after it’s done I start round it’s overall bad practice to use them as you run into risk of client not getting result from client
Thanks for the tip tho