Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I change the player's speed when they spawn?

Asked by 10 years ago

I want to make a game that involves running all the time. I think it has something to do with this:

game.Workspace.Player.Humanoid.Running:connect(function(speed)
    if speed > 0 then
        print("Player is running")
    else
        print("Player has stopped")
    end
end)

Although I'm not sure.

Thanks!

1 answer

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
10 years ago

The Humanoid.Running event that you refer to fires when the Humanoid starts or stops running, not when the Player spawns.

If you want to adjust the speed of each player when they spawn from a LocalScript, all you'd need to do is this:

repeat wait() until game.Players.LocalPlayer
player = game.Players.LocalPlayer

repeat wait() until player.Character
character = player.Character

repeat wait() until character:FindFirstChild("Humanoid")
humanoid = character.Humanoid

humanoid.WalkSpeed = 25 --Set the speed that you want them to have

The important thing to bear in mind when writing behaviour that should initiate when a Player spawns is to wait for the Player's Character and Humanoid before trying to modify them.

If you wanted to adjust the WalkSpeed of each Player using a global Script, you would subscribe to the PlayerAdded event of the Players GameService, and then for each Player reference that you get subscribe to the CharacterAdded event:

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        while not character:FindFirstChild("Humanoid") do wait() end
        character.Humanoid.WalkSpeed = 25 --Set the speed
    end)
end)
Ad

Answer this question