How do you change a player's walkspeed through a script?

I’m no scripter, but I like to think I can script basic things. Well, I can’t. I just mess up somehow.

The Problem

I’m trying to change a player’s walkspeed through a script in ServerScriptService. Here is how the script looks:

wait (?) 
game.StarterPlayer.CharacterWalkSpeed = 50

It just doesn’t work once the time the script’s wait time is over. I also tried putting the script in StarterPlayer and changing the script to:

wait (?)
script.Parent.CharacterWalkSpeed = 50

This doesn’t work either. Can someone help me?

20 Likes

You have to change their WalkSpeed in the Humanoid.

You can achieve this by doing this:

local Player = game.Players.PlayerYouIndex

-- Check if they have a Character
if Player.Character then
 -- Get the humanoid and set the walkspeed
 Player.Character.Humanoid.WalkSpeed = 50
end

API References:
Player
https://developer.roblox.com/en-us/api-reference/class/Player
Humanoid
https://developer.roblox.com/en-us/api-reference/class/Humanoid

21 Likes

Well, WalkSpeed is a property of the player’s character’s humanoid object, to change it, you would do something like

Plr.Character.Humanoid.WalkSpeed = 23

However, on a serverscript, you would have to get the Player via alternative means, like

game.Players:GetPlayers()

or

game.Players.PlayerAdded:Connect(function(plr)end)

An example would look like something along the lines of:

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
       local humanoid = char:WaitForChild("Humanoid")
       humanoid.WalkSpeed = 25
    end
end)
11 Likes

If you want to change every player’s walkspeed in the game do

local newWalkspeed = 10 

game.Players.PlayedAdded:Connect(function(player)
   player.CharacterAdded:Connect(function(character)
      character:WaitForChild("Humanoid").WalkSpeed = newWalkspeed
   end)
end)

If you want me to explain how this works I will, just ask, but I assume you’re more just interested in having something to copy and paste in

8 Likes

What I want to do is change the player’s walkspeed every once in a while.

If you want to do that, it would be best to use either PlayersService:GetPlayers() or use FireServer and OnServerEvent /InvokeServer and OnServerInvoke on RemoteEvents/RemoteFunctions respectively, which have the player that the event was fired from as the first parameter.

You can find out more here:

remote events and functions

players service

Edit: Touched Events can also be a viable option aswell, if you, for example want a player to get faster/slower whenever they touch a part

4 Likes

Specify every once in a while. What are the criteria that the player has to meet that their WalkSpeed should be changed?

This is just an example code you can fit to your script (as posted above)

local function ChangeWalkSpeed(Player, NewWalkSpeed)
 if Player and Player.Character then
  if Player.Character:FindFirstChildWhichIsA("Humanoid") then
   Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = NewWalkSpeed
  end
 end
end
5 Likes

They don’t have to meet any criteria for their walkspeed to change, it’s automatic. Sorry for not being very clear.

1 Like

I edited my reply with a function you can use to change a Player’s WalkSpeed. Just call it like this:

ChangeWalkSpeed(game.Players.Pancake1824, 50)

That checks if the Player has a Character and a Humanoid, if they do, change their WalkSpeed to the one you desire.

4 Likes

Ok, thank you so much. I will mark your post as the solution because it’s what I wanted. And to everyone else, thank you so much for replying!

Why not hold the Humanoid in a variable to prevent a double look-up? Seems pointless to write out the find statement twice over.

local function ChangeWalkSpeed(Player, NewWalkSpeed)
    if Player and Player.Character then
        local Humanoid = Player.Character:FindFirstChildWhichIsA("Humanoid")
        if Humanoid then
            Humanoid.WalkSpeed = NewWalkSpeed
        end
    end
end
4 Likes

[Solved] How can you make the walk speed increase multiple times when you touch it? For example, my current Walk speed is 16 but I add 10 to that when the player touches a part and when they touch it again it will make it go up 10 more. So, if I add 10 to 16 it will be 26 but then I add 10 again when they touch the part again and it becomes 36. Nevermind, My question has been solved!

1 Like