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
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 )
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.
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