What is wrong with this code?

Please help me! :hugs:

Two issues.

Your variable PlayerSpeed is a reference to the value of a property not the property itself.
If you want to change the walkspeed of the player you will have to modify their Humanoid’s WalkSpeed property.

local Player = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
    if (input.KeyCode == Enum.KeyCode.LeftShift) then
        Player.Character.Humanoid.WalkSpeed = 36
    end
end)

UserInputService.InputEnded:Connect(function(input)
    if (input.KeyCode == Enum.KeyCode.LeftShift) then
     Player.Character.Humanoid.WalkSpeed = 16
    end
end)
1 Like

Several things actually

  1. You don’t change the player’s speed through StarterPlayer during Runtime
  2. Changing the variable doesn’t change the player’s speed, you were changing the value of the variable
  3. You never checked if the player was typing in something like chat
-- local script
local character = script.Parent -- get the player's character
local humanoid = character:WaitForChild("Humanoid") -- get the player's humanoid

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(keycode, gp)
   if gp then return end -- if the player is typing in a textbox or something, stop the function

   if keycode.KeyCode == Enum.KeyCode.LeftShift then
       humanoid.WalkSpeed = 36 -- change the speed through the HUMANOID instance
   end
end)

userInputService.InputEnded:Connect(function(keycode, gp)
   if gp then return end

   if keycode.KeyCode == Enum.KeyCode.LeftShift then
       humanoid.WalkSpeed = 16 -- change it back to the default speed
   end
end)

This is a local script parented under StarterCharacterScripts

2 Likes

Thank you so much!
I didn’t know I have to do it that way, I though I can get away doing it the way I did.

StarterPlayer.CharacterWalkSpeed modifies the walkspeed when for when they next respawn.

Thank you a lot for your help. I didn’t know I can’t change the speed through only using a variable.