Sprint animation still playing when letting go of W

Howdy devs!

So I have a sprint script here, it works fine but when
I start sprinting ( Holding the Shift > W key) but when I let go of the W key the sprint animation still plays. How do I fix this

Video example:

Here’s a part of the code:

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then 
		if humanoid.MoveDirection.Magnitude > 0 then
			humanoid.WalkSpeed = RunSpeed
			PlayAnim:Play()
			tween1:Play()
		end
		
		uis.InputEnded:Connect(function(input) 
			if input.KeyCode == Enum.KeyCode.LeftShift then
				humanoid.WalkSpeed = DefaultSpeed
				PlayAnim:Stop()
				tween2:Play() 
			end
		end)
	end
end)

There is probably no possible fix for this, this is an issue on the physics end, and you would probably need to make complicated code to fix it.
Plus your still pressing shift so its still playing, just stop it when you stop pressing w or smth if your mad about that lol
(Note: This is a theory, not a fact)

first off put that outside of uis.InputBegan

uis.InputEnded:Connect(function(input, gameProccessed) 
	if input.KeyCode == Enum.KeyCode.LeftShift and not gameProccessed then
		humanoid.WalkSpeed = DefaultSpeed
		PlayAnim:Stop()
		tween2:Play() 
	end
end)

and change it a bit

You either can check the humanoid state or check of 2 keys pressing at once.
Try checking the humanoid state

if humanoid:GetState() == Enum.HumanoidStateType.Running then
    humanoid.WalkSpeed = RunSpeed
	PlayAnim:Play()
	tween1:Play()
end
1 Like

this will never work becuase InputBegan fires when the key is pressed and passes this key to function means if u press shift and w input parameter will be assigned to Enum.KeyCode.LeftShift but already inside another function Enum.KeyCode.W

or you can use alternative

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then

		humanoid.WalkSpeed = RunSpeed
		PlayAnim:Play()
		tween1:Play()

		repeat RunService.Heartbeat:Wait()
			if humanoid.MoveDirection.Magnitude > 0 then
				if not PlayAnim.IsPlaying then
					PlayAnim:Play()
				end
			elseif PlayAnim.IsPlaying then
				PlayAnim:Stop()
			end
		until not uis:IsKeyDown(Enum.KeyCode.LeftShift)

		humanoid.WalkSpeed = DefaultSpeed
		PlayAnim:Stop()
		tween2:Play() 

	end
end)
3 Likes
uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then 
		if humanoid.MoveDirection.Magnitude > 0 then
			humanoid.WalkSpeed = RunSpeed
			PlayAnim:Play()
			tween1:Play()
		end
		
		uis.InputEnded:Connect(function(input) 
			if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.W then
				humanoid.WalkSpeed = DefaultSpeed
				PlayAnim:Stop()
				tween2:Play() 
			end
		end)
	end
end)

this should help. it didnt work cuz ur not detecting if the player stopped pressing W

1 Like

There’s still a memory leak in this code, a new connection is made to the ‘uis.InputEnded’ event each time the ‘uis.InputBegan’ event is fired.

hmm then why not use humanoid:GetPropertyChangedSignal("MoveDirection")?

local Game = game --Local environment variable.
local UserInput = Game:GetService("UserInputService") --Services.
local Players = Game:GetService("Players")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")

local function OnHumanoidRunning(Speed)
	if Speed > 0 then
		if UserInput:IsKeyDown(Enum.KeyCode.LeftShift) then
			--Sprint.
		else
			--Disable sprint.
		end
	else
		--Disable sprint.
	end
end

Humanoid.Running:Connect(OnHumanoidRunning)

You could instead check if the humanoid’s move direction’s magnitude is greater than 0 too.

1 Like

bro first of all whats the point of doing Game = game ?
also thats a nice use of the sort of forgotten function :IsKeyDown()
also why do you make long variables just abreviate them ya know

ye ik

bro first of all whats the point of doing Game = game ?

Local environment integration, I also prefer naming scheme consistency, i.e; words are capitalised.

also thats a nice use of the sort of forgotten function :IsKeyDown()

Yes, it’s good for ‘in-the-moment’ keystroke detection.

also why do you make long variables just abreviate them ya know

Variables should be given proper names that accurately describe them, for the purpose of readibility.

you know there is a quote function here ;/

Yes, I don’t like how it formats text blocks though.

To keep this post on topic I suggest you take a look at the following thread, it’s a good resource on garbage collection/memory leaks in Roblox Lua.

oh ok thanks! and yea i guess ur right we cant talk here.

I’ve tried it but I’ve got some tweening issues but I fixed it already.
I think this should do it. Thank you!