For Loop apparently doesn't loop through all players

My main script is supposed to change all players WalkSpeed and JumpPower to 0 before teleporting them into the game map, but it seems it only removes the WalkSpeed and JumpPower for 1 player and not everyone, and i have no idea why.

while true do
	local plrs = game.Players:GetPlayers()
	if #plrs > 1 then
	t = 10
		t = t-1
		s.Value = "Intermission "..t
	until t == 0
	s.Value = "Loading..."
	script.StartingGame.Value = "True"
	local plrs = game.Players:GetPlayers()
	for i = 1, #plrs do
	plrs[i].Character.Speed.Disabled = true -- Disable the script that give more WalkSpeed when walking
	plrs[i].Character.SpeedFoV.Disabled = true -- Disable the FoV changer script
	plrs[i].Character.Humanoid.WalkSpeed = 0
	plrs[i].Character.Humanoid.JumpPower = 0
	local mapselect = game.ReplicatedStorage.Games:GetChildren()
	local choose = math.random(1, #mapselect)
	curnum = 0
	for i =1,#mapselect do
		curnum = curnum + 1
		if curnum == choose then
			mapselect[i]:Clone().Parent = workspace
			curmap = mapselect[i].Name
			script["Chosen Map"].Value = mapselect[i].Name
	local plrs = game.Players:GetPlayers()
	for i = 1,#plrs do
		local num = math.random(1,12)
		plrs[i].Character.Head.CFrame =["Part"..num].Position)
		plrs[i].Character.Parent = workspace.Ingame		
	script.StartingGame.Value = "False"
	s.Value = "Ready?"
	s.Value = "RUSH"
	plrs[i].Character.Speed.Disabled = false
	plrs[i].Character.SpeedFoV.Disabled = false
	plrs[i].Character.Humanoid.WalkSpeed = 16
	plrs[i].Character.Humanoid.JumpPower = 50
	t=160 -- Round starting time

I know i could do this in other ways but i want to make it work with a For Loop

Note: It only changes the WalkSpeed and JumpPower to 1 person but when it comes to teleporting everyone to the game it loops through every player completely fine

1 Like

why does this loop not end before you choose a map…?

You are most likely literally running all of your map code for every single player in that list instead of, you know, just doing that and then running your map code.

Also, not all players will have a Character the moment they load into the game, so this will create errors.
Also also, try a loop like this instead if this is what you are trying to do:

for _, player in ipairs(Players:GetPlayers()) do
    if player.Character and player.Character:FindFirstChild("Humanoid") then
        --player.blah = bleh

You can also check their speed in those scripts instead of disabling them. Usually you should handle things inside of the script itself because disabling running scripts could lead to, as gman puts it, unforseen consequences.


Please indent properly and show your full code. It’s hard to tell what’s going on in your code, but you’re probably breaking your loop somewhere down the line.


OOF i missed that “end” (I know this is obvious but i am new to For Loops and stuff)

Also thanks for the tip, i’ll do it that way.

Thank you for you help, it’s fixed :slight_smile: