Whys my for loop to set a players speed not working?

For some reason my for loop isn’t doing anything. It runs (I tested with print statements) and the player variable inside the list is correct. So not sure why its not working?? I get no error messages in output.

-- Stopping players from being able to move
	for i = team1Num, 1, -1 do
		team1[i].Character.Humanoid.WalkSpeed = 0
		team2[i].Character.Humanoid.WalkSpeed = 0
	end

Well it’s hard to tell without knowing more about team1 and team2’s structure

Function that gets the player that touches a part (I have a few of these for different parts)

T1P1.Touched:Connect(function(touchedPart) -- TOUCHED PART IS THE PART OF THE CHARACTER THAT TOUCHES THE PART, SO BY GETTING ITS PARENT, I'M GETTING THE PLAYERS CHARACTER
	local Player = Players:GetPlayerFromCharacter(touchedPart.Parent) -- FUNCTION TO GET THE PLAYER FROM CHARACTER
	
	if T1Player1 == nil then
		T1Player1 = Player
		T1Player1Prev = Player
		T1B1.BrickColor = BrickColor.new("Teal")
		T1P1.Transparency = 0.75
		T1P2.Base.Transparency = 0
		checkReady()
	end
end)

– This is where i add the player variables into the list:

if T1Player1 and T1Player2 and T1Player3 and T2Player1 and T2Player2 and T2Player3 then
		gamePlayers = {T1Player1, T1Player2, T1Player3, T2Player1, T2Player2, T2Player3}
		
		for i = 6, 1, -1 do
			gamePlayers[i].PlayerGui.ScreenGui.Enabled = true
		end
		
		team1 = {T1Player1, T1Player2, T1Player3}
		team2 = {T2Player1, T2Player2, T2Player3}
		numPlayers = 6
	elseif T1Player1 and T1Player2 and T2Player1 and T2Player2 and T1Player3 == nil and T2Player3 == nil then
		gamePlayers = {T1Player1, T1Player2, T2Player1, T2Player2}
		
		for i = 4, 1, -1 do
			gamePlayers[i].PlayerGui.ScreenGui.Enabled = true
		end
		
		team1 = {T1Player1, T1Player2}
		team2 = {T2Player1, T2Player2}
		numPlayers = 4
	elseif T1Player1 and T2Player1 and T1Player2 == nil and T1Player3 == nil and T2Player2 == nil and T2Player3 == nil then
		gamePlayers = {T1Player1, T2Player1}
		
		for i = 2, 1, -1 do
			gamePlayers[i].PlayerGui.ScreenGui.Enabled = true
		end
		
		team1 = {T1Player1}
		team2 = {T2Player1}
		numPlayers = 2

You know you can check the number of things in a table by doing #TABLE_NAME right?

I have code right after the setting walkspeed part that uses the variables inside the lists and those all work perfectly

for i = team1Num, 1, -1 do
		team1[i].Character.Humanoid.WalkSpeed = 0
		team2[i].Character.Humanoid.WalkSpeed = 0
	end
	
	-- Setting the players teams so they can't kill their teammates
	for i = team1Num, 1, -1 do
		team1[i].Team = Teams["Team1"]
	end
	for i = team2Num, 1, -1 do
		team2[i].Team = Teams["Team2"]
	end
	
	-- Teleporting players into the arena
	for i = team1Num, 1, -1 do
		if team1Num == 1 or team1Num == 3 then
			workspace[team1[i].Name].HumanoidRootPart.CFrame = team1Teleports[i].CFrame
		elseif team1Num == 2 then
			workspace[team1[i].Name].HumanoidRootPart.CFrame = team1Teleports[i+1].CFrame
		end
	end
	for i = team2Num, 1, -1 do
		if team2Num == 1 or team2Num == 3 then
			workspace[team2[i].Name].HumanoidRootPart.CFrame = team2Teleports[i].CFrame
		elseif team1Num == 2 then
			workspace[team2[i].Name].HumanoidRootPart.CFrame = team2Teleports[i+1].CFrame
		end
	end

Like the players are assigned to the teams fine and they are teleported fine.

Well, first off let’s try doing some print statements inside of your for loop and try to check if you can print out their walkspeeds

Both print zero

-- Stopping players from being able to move
	for i = team1Num, 1, -1 do
		team1[i].Character.Humanoid.WalkSpeed = 0
		team2[i].Character.Humanoid.WalkSpeed = 0
		print(team2[i].Character.Humanoid.WalkSpeed)
		print(team1[i].Character.Humanoid.WalkSpeed)
	end

Well we know the loop is working, so the players are still able to move?

Do you have anything else that could be conflicating with that?

Yep they get teleported into the arena and then both can walk, even thought they shouldn’t be able to

Ill have another good look at my code and see

Wait maybe instead of setting their walkspeed to 0, you could try anchoring them?

Do you have a loop anywhere that is constantly setting their walkspeed?

I’ve just had an idea. I have a countdown gui, but that isn’t showing up either… If that for loop wasn’t working there would be basically no time gap between the speed getting set to 0 and back to 16.

for i = numPlayers, 1, -1 do
		gamePlayers[i].PlayerGui.Countdown.Enabled = true
	end
	
	for i = 3, 1 -1 do
		if numPlayers == 6 then
			gamePlayers[1].PlayerGui.Countdown.Frame.TextLabel.Text = i
			gamePlayers[2].PlayerGui.Countdown.Frame.TextLabel.Text = i
			gamePlayers[3].PlayerGui.Countdown.Frame.TextLabel.Text = i
			gamePlayers[4].PlayerGui.Countdown.Frame.TextLabel.Text = i
			gamePlayers[5].PlayerGui.Countdown.Frame.TextLabel.Text = i
			gamePlayers[6].PlayerGui.Countdown.Frame.TextLabel.Text = i
		elseif numPlayers == 4 then
			gamePlayers[1].PlayerGui.Countdown.Frame.TextLabel.Text = i
			gamePlayers[2].PlayerGui.Countdown.Frame.TextLabel.Text = i
			gamePlayers[3].PlayerGui.Countdown.Frame.TextLabel.Text = i
			gamePlayers[4].PlayerGui.Countdown.Frame.TextLabel.Text = i
		elseif numPlayers == 2 then
			gamePlayers[1].PlayerGui.Countdown.Frame.TextLabel.Text = i
			gamePlayers[2].PlayerGui.Countdown.Frame.TextLabel.Text = i
		end
		task.wait(1)
	end
	
	for i = numPlayers, 1, -1 do
		gamePlayers[i].PlayerGui.Countdown.Enabled = false
	end
	
	-- Resetting all the players walkspeed
	for i = #gamePlayers, 1, -1 do
		gamePlayers[i].Character.Humanoid.WalkSpeed = 16
	end

Ill add prints to check this runs

Yeah I’m certain that is probably why it isnt working

Just seen i missed the comma on that for loop :man_facepalming: , hopefully that will fix it all. Thank you very much for your help :slight_smile:

How would I do this actually? Think its a better way. Would I just do player.Character.HumanoidRootPart.Anchored = true?

Yeah thats all you would do but if your playing animations on the characters when they are staning still you may not want to do this