How to use Bindable.Event:Wait() in a loop?

Hi everyone,

I’m having small problem with my game. I’m trying to make it so that a function will be looping until a BindableEvent is called.

Here is an example:

while true do
    gameFunction() -- I want this to keep looping until the event is called
local winnerFound = roundEnd.Event:Wait(1)
if winnerFound == true then
    break
end

However it appears that it just keeps waiting indefinitely… is there a better way to do this? roundEvent is waiting until there is one player left to be called. I thought about using :Connect, but I’m not entirely sure how to implement that in a loop.

1 Like

Event:Wait() just waits until its fired again, so you just need to fire that event from the script when you want to continue.

1 Like

you can do something like this:

local winnerEvent = game:GetService("ReplicatedStorage").Event
local eventConn
local eventWinner

eventConn = winnerEvent.Event:Connect(function(winnerPlr)
	eventConn:Disconnect()
	eventWinner = winnerPlr
end)

while not eventWinner do
	print("do game function here")
	task.wait()
end

print("the winner is:", eventWinner)
while true do
    gameFunction()
    roundEnd.Event:Wait()
    break
end

Any wait will yield the thread, making it so you don’t need an if statement, it just will run when the wait’s time has elapsed!

Also, passing a number in roundEnd.Event:Wait() does virtually nothing, it’s not normal wait. It will wait until the event is fired, then continue the thread.

1 Like

Sorry, for the late reply. I found that Brain’s solution best fit my situation, and after a few modifications I was able to make it work. Thank you guys for replying!

Unfortunately, this does the same thing as my original script did :frowning: