Do events fire out of repeat loops?

So I have a simple question, do events fire out of repeat loops? What I mean by that is:

Take this bit of code.

local var = 0

repeat
print("haha yes this is epics code")
var = var + 1
wait(1)
until var == 100

game.Players.PlayerAdded:Connect(function()

print("plr has join")

end)

Will the .PlayerAdded event fire even when the repeat loop is happening?

yes, but don’t do that because then you’ll have like 90 .PlayerAdded events that will probably crash the server

EDIT: i thought he meant in the repeat loop

No it will not. You have not connected the even yet and have the repeat loop running. When the loop ends the PlayerAdded event connects and listens to players joining on a new thread

So if I connect it prior to the repeat loop, it will run then?

It will not! This is because your code hasn’t begun reading the part where the event connection is made, meaning it doesn’t know that it exists yet. Placing the event connection above the repeat statement should make it run even when the loop is active.


This doesn’t have anything to do with your question, but using repeat loops like this is quite ineffectient. Most developers make use of for loops for incremental values, you can read more about them here.

2 Likes

Yes.

Think of it like this

local t = 0

while t < 10 do
    print("In loop")
    t = t + 1
    wait(1)
end

print("Connecting player added event")
game.Players.PlayerAdded:Connect(function(plr)
    print(plr.Name.." has joined the game")
end)

If you run that code, you will see that the loop will run, after around 10 seconds it will end the loop and listen to players added in game.

If you were wanting to make the player added event run while the loop is running above, you would need to make a new thread or connect the function above the loop.

The code did not reach the event yet and does not know.

1 Like