For i loops don't work in functions?

So I was making a minigame, and one part of the teleporting players did not work. The script: local function teleportplayerstospawns()
game.ReplicatedStorage:WaitForChild(“Stats”).Value = “Teleporting All Players”
print(“went”)
for i = 1,#allplayers do
print(i)
print(“STARTED”)
wait(0.1)
end
end
Script is in serverscript server.
Error: The for i loops does not run.
Please help me, thank you.

In order to help you fix this issue, I’ll need more context. Where is #allplayers defined?

1 Like

All players is defined as a variable: local allplayers = players:GetChildren().

You should probably define that right before your loop starts. I’m guessing that allplayers is collected before any players join, thus is empty. Therefore, your loop wouldn’t run because the length of allplayers is 0.

function teleportplayerstospawns()
	game.ReplicatedStorage:WaitForChild("Stats").Value = "Teleporting All Players"
	print("went")
	local allplayers = game.Players:GetPlayers()
	for i = 1,#allplayers do
		print(i)
		print("STARTED")
		wait(0.1)
	end
end
6 Likes

OH, I see now. I am so dumb. Thank you alot.