Setting default walkspeed

Hello, I was trying to set the player’s default walkspeed

local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local player = Players.LocalPlayer

local sprintspe = 16
local walkspe = 9

player.Character.Humanoid.WalkSpeed = walkspe

however, I was met with

Players.SkyRiseTH11650.PlayerScripts.LocalScript:10: attempt to index nil with 'Humanoid' 

Can anyone see what is the issue with this?

1 Like

Player’s character haven’t loaded in yet.

Try putting a variable with value as player.Character or player.CharacterAdded:Wait() as if there were no character found (the first value were false or nil), it’ll switch to the second one which is waiting for an event to return the character first, thus should not throw an error.

If you ask why can’t you put only player.CharacterAdded:Wait(), it is because there are still chances that the character managed to load in, and it will wait indefinitely as it did not register the event in time before it fired.

local character = player.Character or player.CharacterAdded:Wait()

Try this instead

local Players = game:GetService("Players")

local UIS = game:GetService("UserInputService")

local player = Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()

local humanoid = character:WaitForChild("Humanoid")

humanoid.WalkSpeed = 16

Looks like the player’s character and humanoid hasn’t loaded in before the script has ran.

Add this line beneath line 5:

local character = player.Character or player.CharacterAdded:Wait()

Then replace line 10 with this:

player.character:WaitForChild("Humanoid").Walkspeed = walkspe

You can also set CharacterWalkSpeed in StarterPlayer, it might be in the Game Settings panel as well. No scripts needed.

1 Like