Hi All,
This is a super easy question (or should be), but like all good questions, I cannot find an answer, so I am turning to DevForum. My question is simple: What would be the best way to repeat a loop, until a remote event is fired? An example of this would be
ReplicatedStorage.Weather.Rainy.OnClientEvent:Connect(function() --once a remote event is fired, does this
repeat
wait(.1)
print("Raining")
until game.ReplicatedStorage.RainEnd.Fired -- this obviously does not work. How would I stop the loop once another remote event is fired?
end)
I could probably toggle it with a variable
local On = true
repeat
print("Raining")
wait(.1)
until local On == false --how would I even toggle this when a remote event is fired? would I just check if it is fired inside of the repeat loop, and then set the variable On?
So, to rephrase, how would I run a loop until a remote event is fired? An additional note: I would check if it is fired in a local script, as it is firing from the server. Thank you in advance
What I found worked best was placing an IntValue in ReplicatedStorage (where both the Server and Client can access it), then when firing the start remoteevent, repeatedly changing the intvalue - 1. Then, I detect the value of the intvalue on the Client, and once it hits 0, stop the loop. Looks something like this:
Server
local RainTimer = ReplicatedStorage.RainLast -- an intvalue in RepStorage
ReplicatedStorage.Weather.Rainy:FireAllClients() -- tells the client the rain has started
while RainTimer.Value > 0 do
RainTimer.Value = RainTimer.Value - 1
wait(1)
end
Client
local RainTimer = ReplicatedStorage.RainLast -- an intvalue in replicatedstorage
ReplicatedStorage.Weather.Rainy.OnClientEvent:Connect(function() -- sentses when rain starts
wait(.1)
while RainTimer.Value > 0 do -- repeats until the rain timer reaches 0
print(RainTimer.Value)
wait(1)
end
end)
Is this an okay solution? Is it too taxing on the Server or Client?
local RainStartStop = game.ReplicatedStorage:WaitForChild("RainStartStop")
local Raining = false
RainStartStop.OnClientEvent:Connect(function(value)
Raining = value
end)
while wait() do
if Raining then
print("Rain")
end
end