Player speed changing on any keypress

I basically am trying to make my running script not interfere with another animation as you can continue holding down the run button and the animation will continue to play as another animation for a different key bind is playing.

So what I thought of doing was:

if input.KeyCode == Enum.KeyCode.LeftShift or Enum.KeyCode.Q then
    player.Character.Humanoid.WalkSpeed = walkSpeed
    PlayAnim:Stop()

shift is for ending the run and pressing q is there to stop it from playing during the other animation

However this caused a problem as when any key or mouse clicked is registered it causes the player to slow down.

Why does it end up doing that and how can I fix it?

2 Likes

Could you show a video displaying this issue?

2 Likes

Bro at least show the whole code bruh cause you declared walkspeed I dont even know what walkspeed is doesnt matter but it helps.Plus is it a script or Local Script

2 Likes

or Enum.KeyCode.Q → or input.KeyCode == Enum.KeyCode.Q

The issue is because in the second part of your conditional statement, you are not comparing anything to Enum.KeyCode.Q, so therefore, as Enum.KeyCode.Q exists, it is returning true for every key you are pressing.

2 Likes

You’ll also want to add gameProcessedEvent into your code as it will register unnecessary inputs when the user is typing in chat.

(UserInputServiceVariable).InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.Q then
player.Character.Humanoid.WalkSpeed = walkSpeed
PlayAnim:Stop()

2 Likes

You should do this instead.

if input.KeyCode == Enum.KeyCode.LeftShift or input.Keycode == Enum.KeyCode.Q then
2 Likes

Please provide video and whole script and explorer.

2 Likes

No need to get angry at him. Just ask him nicely.

1 Like

Yeah sorry for not providing the whole code here it is:


local UIS = game:GetService('UserInputService')
local Players = game:GetService('Players')
local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()

local sprintSpeed = 40
local walkSpeed = 16

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        player.Character.Humanoid.WalkSpeed = sprintSpeed
        local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://10631428562'
        PlayAnim = Char.Humanoid:LoadAnimation(Anim)
        PlayAnim:Play()
    end
end)

UIS.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift or Enum.KeyCode.Q then
        player.Character.Humanoid.WalkSpeed = walkSpeed
        PlayAnim:Stop()
    end
end) 

its a local script in startercharacterscript and here is a video of it happening:

2 Likes

Thanks! I look into it. Hope i can help.

1 Like

Sorry for the lag spike after 15 secs but i think you guys get the point

1 Like

Where is PlayAnim Defined? Can’t see it, maybe im blind lol

1 Like

Also, you may want to lock shiftlock to off in game setting as it seems to be interfering with the script.

Might help- even if it doesn’t is so annoying for a player who left shiftlock on.

1 Like

I have an idea: When you play the running anim, stop all others… may or may not work

1 Like

I have a script for shift lock so it uses a different keybind

1 Like

ill try that for the other scripts. I stop the animations while shift is being held

1 Like

Glad i could help. Have fun building!

1 Like

I aint mad LOL thats how I speak sorry if it felt like that

1 Like

nah man you didn’t do anything wrong, kinda on me for not showing the script

2 Likes