Pressing other keys messes up InputEnded

Hello, i am trying to make a simple script that makes the player sprint, but when in the sprinting state, when i press any keys or mouse buttons it stops the sprinting.

here is an example of what i am talking about

there are no errors, and i believe i have set up the if statements correct, but i might be incorrect
any solutions?

Code


local UIS = game:GetService("UserInputService")
local Sprinting = false

local Anim = script:WaitForChild("Animation"):Clone()
Anim.Parent = character
local SprintAnim = humanoid:LoadAnimation(Anim)
SprintAnim:AdjustSpeed(0.6)

UIS.InputBegan:Connect(function(input, isTyping)
	if UIS:IsKeyDown(Enum.KeyCode.W) and UIS:IsKeyDown(Enum.KeyCode.LeftShift) then
		if Sprinting == false then
			if isTyping then return end
			Sprinting = true
			humanoid.WalkSpeed = 20
			SprintAnim:Play()
		end
	end
end)

UIS.InputEnded:Connect(function(input, isTyping)
	if input.KeyCode == Enum.KeyCode.W or Enum.KeyCode.LeftShift then
		if Sprinting == true then
			if isTyping then return end
			Sprinting = false
			humanoid.WalkSpeed = 8
			SprintAnim:Stop()
		end
	end
end)

Any help is greatly appreciated

In the input ended event, Change

if input.KeyCode == Enum.KeyCode.W or Enum.KeyCode.LeftShift then

To

if input.KeyCode == Enum.KeyCode.LeftShift then
1 Like

Thank you that seemed to work, but could you explain to me why my original code doesnt work if you dont mind, im trying to learn

Because with your code, if he leaves the w and clicks on D or any others letter he won’t be able to sprint.

that was the point of the script, so i didnt have to check if the player had momentum and stop the animation from playing, i had it set so if the player stops holding w then if would stop sprintg

I mean if he tried running sideways he won’t be able to, except if he goes diagonally

that was the point, it was made so the i can only sprint while holding w, so i couldnt sprint backwards, only forwards

1 Like

Try doing this then

if input.KeyCode == Enum.KeyCode.W or input.Keycode == Enum.KeyCode.LeftShift then
1 Like

thats what originally did, as you can see in the code i gave, but then problems arise when i try to press any other button while sprinting it stop the sprinting state

It’s a bit different just try it

that seems to work, thank you, i didnt think the input.keycode was needed. i appreciate the help alot

It is a weird thing when using or when trying ur code you mean that
Enum.KeyCode.LeftShift ~= nil since you didn’t specify what it was related to, so if you said

if text == "aa" or "bb" then
print("ABC")
end

It will always print even if the text wasn’t aa since
or "bb" means if bb ~= nil, I should have explained it like that earlier, sry for that.

1 Like

I hope you understood what I meant here.

1 Like

Ah I see now, thank you that makes so much more sense, I really appreciate the help a lot!

1 Like