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 11 years ago

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

1game.Workspace.Player.Humanoid.Running:connect(function(speed)
2    if speed > 0 then
3        print("Player is running")
4    else
5        print("Player has stopped")
6    end
7end)

Although I'm not sure.

Thanks!

1 answer

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
11 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:

01repeat wait() until game.Players.LocalPlayer
02player = game.Players.LocalPlayer
03 
04repeat wait() until player.Character
05character = player.Character
06 
07repeat wait() until character:FindFirstChild("Humanoid")
08humanoid = character.Humanoid
09 
10humanoid.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:

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

Answer this question