My game contains events. When an event occurs, I want to use RemoteEvent:FireAllClients(), create a UI with text, and wait until the script in the client has completed before the next event occurs. Essentially, I’m attempting to make it so that when a remote event is fired, it waits until the fired event has completed before proceeding.
I know I could utilize remote functions in a loop and fire it for each player, but I’m pretty sure that will cause my game to lag.
Here is an example of the script I have right now:
You could fire the remote event back to the server at the end of the OnClientEvent function in your local script, and also create a number variable in the ServerScript (or Module Script) to count how many times the event has fired back to the server. Looking at the given code, it appears you want to fire the remote event 6 times.
I just want to ask one thing however, are you using one remote event, as well as one LocalScript for this? If you are using more than one LocalScript and/or RemoteEvent, you should create another RemoteEvent to fire at the end of your LocalScripts, and just send 1 as the argument to add up the count on the server.
The given example is only for one LocalScript and one RemoteEvent.
So here’s an example:
Local Script:
local event = -- your RemoteEvent
event.OnClientEvent:Connect(function(name, args)
-- your code
-- at the end
event:FireServer(1) -- this number will be added to the 'count' in the 'OnServerEvent' function
end)
Server Script (although it appears you are using a Module script)
local event = -- your Remote Event
local count = 0
event.OnServerEvent:Connect(function(plr, num)
if num ~= nil and num == 1 then
count += 1
if count < 6 then -- since you fire the remote event 6 times
event:FireAllClients() -- your arguments
end
end
end)
I’m not quite sure if this is an ideal and effective method, but you may still try.
Edit: I actually might have overcomplicated this. Instead of creating a number variable, you can just wait until the new RemoteEvent gets fired back to the server by using this function remoteEvent.OnServerEvent:Wait(), and then fire the next remote event etc. Make sure you have created a new RemoteEvent and fire that event at the end of the OnClientEvent function in your LocalScript(s).
So you’d do something like this:
Local Script
local MainEvent = -- Your main RemoteEvent
local OtherEvent = --Your new RemoteEvent
MainEvent.OnClientEvent:Connect(function(name, args)
-- your code
-- at the end
OtherEvent:FireServer() -- fires the new RemoteEvent
end)
Server Script (or Module Script)
function EventModule:Initialize()
local otherEvent = -- Locate your new RemoteEvent
print("Events coming online...")
task.wait(30)
rep.RemoteEvents.EventRemote:FireAllClients(name, args)
otherEvent.OnServerEvent:Wait() -- yields the code until this event is fired
rep.RemoteEvents.EventRemote:FireAllClients(name, args)
otherEvent.OnServerEvent:Wait() -- does the same for the rest of the code
rep.RemoteEvents.EventRemote:FireAllClients(name, args)
otherEvent.OnServerEvent:Wait()
rep.RemoteEvents.EventRemote:FireAllClients(name, args)
otherEvent.OnServerEvent:Wait()
rep.RemoteEvents.EventRemote:FireAllClients(name, args)
otherEvent.OnServerEvent:Wait()
rep.RemoteEvents.EventRemote:FireAllClients(name, args)
end
The best way to do this is by using a remote function. The code that invoked the remote will yield until the function invoked runs, which is useful for returning information.
I do not believe it will cause lag. It seems like it would be no different than firing an event from the client and then the server firing back. Your code will just yield until the function is over, unless you put the invoke inside of a task.spawn(), as you demonstrated.
Have you tried using the method I have given above (in my second post)? I’d understand and accept if you would probably rather use a RemoteFunction instead, however, this topic was mainly about if there is a way to wait for a remote event script to finish.
Using @BabyNinjaTime’s method is good to use if you want to use RemoteEvents and wait for each player’s response individually. You would just write this:
for _, player in ipairs(Players:GetPlayers()) do
remote:FireClient(player)
local response = false
local connection
connection = remote.OnServerEvent:Connect(function(playerWhoFired)
if playerWhoFired ~= player then
return
end
response = true
connection:Disconnect()
end)
repeat
task.wait()
until response
end