I have a local script in StarterPlayerScripts which in short is a shift to sprint.
However I want the sprint animation to play only when the player is both moving and holding LeftShift.
Including the ability to replay the animation even if you have previously stopped moving before, but are still holding down shift.
My main attempt at this was to start a runservice when pressing shift, which would connect to a function that fires whenever the magnitude of the player changes; stopping and playing the sprint animation whenever. After that, when unpressing LeftShift, it would set your walkspeed back to normal, disconnect the runservice, and stop the animation if playing.
Yet, when holding shift the animation constantly plays again, making it looks very choppy and buggy; when unpressing LeftShift, the function is still in effect with the same problems.
Am I doing something wrong? Is this a wrong way to approach this problem?
-- Function that checks magnitude and applies animation (also changes speed)
function CheckIfMoving()
humanoid.WalkSpeed = runspeed
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if humanoid.MoveDirection.Magnitude == 0 then
Run_Track:Stop()
else
Run_Track:Play()
end
end)
end
-- Variable to store runservice
local CurrentCheck
-- On LeftShift start a runservce that connects to the function
UIS.InputBegan:Connect(function(input, gp) -- im gonna shoot myself
if input.KeyCode == Enum.KeyCode.LeftShift and not gp then
CurrentCheck = RunService.Heartbeat:Connect(CheckIfMoving)
end
end)
-- On LeftShift end disconnect the runservice, set walkspeed to normal and stop animation
UIS.InputEnded:Connect(function(input, gp)
if input.KeyCode == Enum.KeyCode.LeftShift and not gp then
CurrentCheck:Disconnect()
humanoid.WalkSpeed = walkspeed
Run_Track:Stop()
end
end)
Ive looked through multiple posts of this (including different methods), but they either didnt cover what I was looking for, or just didnt fully work.
Sorry if this was hard to understand/didnt right this correctly.
I’ll accept any other approaches to this problem.
Appreciate the help!