Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my walk speed changer script not working?

local function SetWalkSpeedForPlayers(Players, WalkSpeed)
 for _, Player in pairs(Players) do
  local Humanoid = (Player.Character.Parent ~= nil) and Player.Character:WaitForChild("Humanoid")
  if Humanoid then
   Humanoid.WalkSpeed = WalkSpeed
  end
 end
end

SetWalkSpeedForPlayers(game.Players:GetPlayers(), 0)
wait(5)
SetWalkSpeedForPlayers(game.Players:GetPlayers(), 16)

I'm not trying to change the walk speed of all the players as soon as they join, but i'm just trying to make a script that changes the walk speed of all the players to 0 and then after 5 seconds go back to the regular 16 walk speed, but my script is not working. Help?

2 answers

✓ Accepted answer
game.Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(function(Character)
		local function SetWalkSpeedForPlayers(Players, WalkSpeed)
			for _, Player in pairs(Players) do
				Player.Character:WaitForChild("Humanoid")
				Player.Character.Humanoid.WalkSpeed = WalkSpeed
				
			end
		end
		
		SetWalkSpeedForPlayers(game.Players:GetPlayers(), 0)
		wait(5)
		SetWalkSpeedForPlayers(game.Players:GetPlayers(), 16)
	end)
end)

I added a PlayerAdded and CharacterAdded event at the beginning of the script so it is run when the character has actually spawned.

On line 4 you put: if Humanoid then

Because the script was run before the character was loaded it was decided that it wasnt humanoid because it simply couldn't find it, and that's where your error came in.

Hey,

Cool! You wanna make your players run like cheetah? :D so, here's how it goes.

First off, I'll begin with defining a basic function that changes the speed of all the players.

function ChangeSpeed(Players, Speed)
    for k,v in pairs(game:GetService("Players"):GetChildren()) do
        if v and v:WaitForChild("Character") and v.Character:WaitForChild("Humanoid") then 
            v.Character.Humanoid.WalkSpeed = Speed
        end
    end
end

Alright, We're good to go with our function yay! let me explain you the lines

Once we defined our function, the next like starts with a loop which will loop through the players currently playing our game an then checks if the player exists (conditional if statement inside the loop) , now if he does, we add another AND condition which would check if the Character of the player is loaded(what if the player has joined the server, but the character hasn't loaded..?) and now we check if the character has a humanoid with another AND condition.

Finally after everything looks fine, we're just a line away from getting our player and giving him the power of le mighty cheetah! RAWR!

This discussion is preserved as it was. New answers, comments, and votes are closed.