Need some help on a walk to run script

Just a little confused on what i got wrong here, doesn’t really work ingame but says nothing’s wrong with it.

local walkSpeed = 16
local runSpeed = 24

local function onKeyPress(player, input, isProcessed)
    if isProcessed then
        return
    end
    
    if input.KeyCode == Enum.KeyCode.LeftShift then
        local character = player.Character
        if character then
            local humanoid = character:FindFirstChild("Humanoid")
            if humanoid then
                humanoid.WalkSpeed = input.UserInputState == Enum.UserInputState.Begin and runSpeed or walkSpeed
            end
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.WalkSpeed = walkSpeed
        
        character:GetPropertyChangedSignal("Parent"):Connect(function()
            if character.Parent == nil then
                humanoid.WalkSpeed = walkSpeed
            end
        end)
    end)
end)

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
game:GetService("UserInputService").InputEnded:Connect(onKeyPress)

1 Like

The InputBegan and InputEnded functions do not pass a player parameter, seeing as they always should be called in LocalScripts.

Try indexing the player as a variable:

local player = game.Players.LocalPlayer

and remove the first parameter of the onKeyPress() function.