How do i change humanoid speed on for i,v in pairs()

hello, i have been struggling on this script like i want to make a match that, if the match isnt starting then all players in the match cant move or disable movement and if the match is starting then the players will start moving, idk what i did wrong, is it because the script or im really an idiot?

i thought it was an easy script but nope haha get noob

for i,v in pairs(game.Players:GetPlayers()) do
     local char = v.Character or v.CharacterAdded:Wait()
     char:FindFirstChild("Humanoid").WalkSpeed = 0
     wait(5)
     char:FindFirstChild("Humanoid").WalkSpeed = 16
end
2 Likes

could you send the output, it would really help me understand where the problem is :)?

You’re waiting 5 seconds to change a single players walkspeed, when cycling through it would take forever.

One solution would to just make a new thread to change it via that.

When you have a Wait in your loop, it waits to call the next index.

There is no error, and i tried adding print and nope there is no printed

I still dont understand, can you explain it little more? thanks

You’re waiting 5 seconds before it changes the next players walk speed

He is trying to say that by adding a wait in the loop, you are waiting 5 seconds for each element in the table, meaning that it would take #elements * 5 seconds to make these changes to all of the humanoids, you should change everyone’s speed to 0 first, then wait 5 (out of the loop), and then change it again to 16 (I hope you understood it :slight_smile:)

Every time that you iterate through all of the players in game, you are waiting five seconds. This means that it will set the first player’s walk speed to 0, wait five seconds, make their walk speed 16, then repeat that same process for every other player. By waiting five seconds for every player, you are yielding the for loop and making the entire process take (5 * the amount of players) seconds.

To fix this, there are a couple things you can do: you can use a delay function in order to set the walkspeed back to 16, you can spawn a new thread for every iteration, or you can simply set everybody’s walkspeed to 16 at a later point in the script by using a separate for loop.

Hope this helped!

1 Like

You could have two loops, like so:

local Players = game:GetService('Players')

for _,v in pairs(Players:GetPlayers()) do
	local Character = v.Character or v.CharacterAdded:Wait()
	local Humanoid = Character:FindFirstChildOfClass('Humanoid')
	if Humanoid then
		Humanoid.WalkSpeed = 50
	end
end

wait(5)

for _,v in pairs(Players:GetPlayers()) do
	local Character = v.Character or v.CharacterAdded:Wait()
	local Humanoid = Character:FindFirstChildOfClass('Humanoid')
	if Humanoid then
		Humanoid.WalkSpeed = 16
	end
end