How to Repeat Until Remote Event Fired?

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

3 Likes

You can :Wait on the event. This will yield the thread until the event is fired.

ReplicatedStorage.RainEnd.OnClientEvent:Wait()
-- code
6 Likes

Okay, this solution is so easy, I have no Idea why I didn’t think of it, thanks! I will try this out!

3 Likes

I would suggest making a function that fires on the event such as:

function event()
--stuff here
end

ReplicatedStorage.Weather.Rainy.OnClientEvent:Connect(event)

as for the loop I would have a variable like denounce that is put through a while true loop that breaks

3 Likes

Okay, I tried something like this, but I will definitely give this a shot, thank you!

1 Like

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?

3 Likes

ServerScript

local RainStartStop = Instance.new("RemoteEvent", game.ReplicatedStorage)
RainStartStop.Name = "RainStartStop"
wait(10)
RainStartStop:FireAllClients(true)
wait(10)
RainStartStop:FireAllClients(false)

LocalScript

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
1 Like