-
What do you want to achieve? Keep it simple and clear!
A few days ago, I made a shift to sprint script. I want to have it with advancements, such as stopping animations when jump (Already did) Or mainly, stopping the running animation when the player has stopped moving
-
What is the issue? Include screenshots / videos if possible!
The issue is, that whenever the player stops moving, the animation continues to play, and I would not like that to happen. Just want to keep it simple, make the animation stop when the player isn’t moving. (P.S, WHILE the player is still Holding down shift , and stops moving, that’s when I would like the animation to stop.
Example:
When the player stops while holding down the shift button, the animation still plays, is there a way to avoid this?
Also, just in case, here is the local script I am using:
local DefaultSpeed = 16
local SprintSpeed = 28
local SprintKey = Enum.KeyCode.LeftShift
local UIS = game:GetService("UserInputService")
local PLayers = game:GetService("Players")
local player = PLayers.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local RunAnimation = Instance.new("Animation")
RunAnimation.AnimationId = "rbxassetid://9584328745"
local PlayRunAnim = Humanoid:LoadAnimation(RunAnimation)
local Running = false
UIS.InputBegan:Connect(function(key, gameProcessed)
if key.KeyCode == SprintKey and gameProcessed == false then
if not Running then
Running = true
if Humanoid.MoveDirection.Magnitude > 0 then
if not PlayRunAnim.IsPlaying then
Humanoid.WalkSpeed = SprintSpeed
PlayRunAnim:Play(0.1)
end
end
end
end
end)
UIS.InputEnded:Connect(function(key)
if key.KeyCode == SprintKey then
Running = false
if PlayRunAnim.IsPlaying then
Humanoid.WalkSpeed = DefaultSpeed
PlayRunAnim:Stop(0.1)
end
end
end)
Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
if Humanoid.FloorMaterial == Enum.Material.Air then
if Running then
repeat wait()
if PlayRunAnim.IsPlaying then
PlayRunAnim:Stop(0.1)
end
until Humanoid.FloorMaterial ~= Enum.Material.Air
if Running then
if Humanoid.MoveDirection.Magnitude > 0 then
if not PlayRunAnim.IsPlaying then
PlayRunAnim:Play(0.1)
end
end
else
Running = false
if PlayRunAnim.IsPlaying then
PlayRunAnim:Stop(0.1)
Humanoid.WalkSpeed = DefaultSpeed
end
end
else
if Running then
if Humanoid.MoveDirection.Magnitude > 0 then
if not PlayRunAnim.IsPlaying then
Humanoid.WalkSpeed = SprintSpeed
PlayRunAnim:Play(0.1)
end
end
end
end
end
end)
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried to look into other posts that regard this topic, but none are what I am looking for. An example of what I found that may or may not be useful is these lines of code (There is another one that is almost the same, or at least pretty sure but I cant find it.)
Image:
If I can use these line of code to help me, how would I be able to do so with my script? I know how the parts work and everything. Only issue is, if I can use this variable and function in my script, how would I be able to do so?
Thanks
Would this work? (Edit: fixed incorrect else placement lol)
if Humanoid.MoveDirection.Magnitude > 0 then
if not PlayRunAnim.IsPlaying then
Humanoid.WalkSpeed = SprintSpeed
PlayRunAnim:Play(0.1)
end
else
PlayRunAnim:Stop()
end
Okay, so I just tried the new script, and nothing happened when the player stopped moving while running, the animation did not stop. There was also no errors afterwards (Sorry for the long delay)
It’s probably because that function only fires when Running is false. While running, it doesn’t check the Humanoid’s MoveDirection again. The placement of this statement is the cause of the issue:
if not Running then
Running = true
Try this instead:
UIS.InputBegan:Connect(function(key, gameProcessed)
if key.KeyCode == SprintKey and gameProcessed == false then
if Humanoid.MoveDirection.Magnitude > 0 then
if not Running then
Running = true
if not PlayRunAnim.IsPlaying then
Humanoid.WalkSpeed = SprintSpeed
PlayRunAnim:Play(0.1)
end
end
else
Running = false
PlayRunAnim:Stop()
end
end
end)
Okay, so I tried using the new line of code, with the function i have commented about earlier, and it worked! Thanks for all your help
1 Like
Another Problem Came Up
I didnt notice this at first, since I wasn’t doing much while I was gone, but anyway. After testing out the new code a bit, I realised there are a few problems with it. Such as jumping, will make the animations stop, and make the player stay at a slow speed, or even if the player moves their camera, or their player the slightest to the left, the player slows down.
I’ve tried to edit the code as much as I could, and cant find a solution.
Except, Im pretty confident its either with the Vector3(0, 0, 0), or it could be the Moved1() Function instead.
Edited version of the code:
--SETTINGS--
local DefaultSpeed = 16
local SprintSpeed = 28
local SprintKey = Enum.KeyCode.LeftShift
local UIS = game:GetService("UserInputService")
local PLayers = game:GetService("Players")
local player = PLayers.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local RunAnimation = Instance.new("Animation")
RunAnimation.AnimationId = "rbxassetid://9584328745"
local PlayRunAnim = Humanoid:LoadAnimation(RunAnimation)
local Running = false
local VectorZero = Vector3.new(0, 0, 0)
local function Moved1()
if Humanoid.MoveDirection ~= VectorZero then
else
print("Stopped Moving")
PlayRunAnim:Stop()
Humanoid.WalkSpeed = SprintSpeed
return
end
end
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(Moved1)
Humanoid.WalkSpeed = DefaultSpeed
UIS.InputBegan:Connect(function(key, gameProcessed)
if key.KeyCode == SprintKey and gameProcessed == false then
if not Running then
Running = true
if Humanoid.MoveDirection.Magnitude > 0 then
if not PlayRunAnim.IsPlaying then
Humanoid.WalkSpeed = SprintSpeed
PlayRunAnim:Play(0.1)
end
end
else
Running = false
Moved1()
end
return Moved1()
end
Humanoid.WalkSpeed = DefaultSpeed
end)
UIS.InputEnded:Connect(function(key)
if key.KeyCode == SprintKey then
Running = false
if PlayRunAnim.IsPlaying then
Humanoid.WalkSpeed = DefaultSpeed
PlayRunAnim:Stop(0.1)
end
end
end)
Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
if Humanoid.FloorMaterial == Enum.Material.Air then
if Running then
repeat wait()
if PlayRunAnim.IsPlaying then
PlayRunAnim:Stop(0.1)
end
until Humanoid.FloorMaterial ~= Enum.Material.Air
if Running then
if Humanoid.MoveDirection.Magnitude > 0 then
if not PlayRunAnim.IsPlaying then
PlayRunAnim:Play(0.1)
end
end
else
Running = false
if PlayRunAnim.IsPlaying then
PlayRunAnim:Stop(0.1)
Humanoid.WalkSpeed = DefaultSpeed
end
end
else
if Running then
if Humanoid.MoveDirection.Magnitude > 0 then
if not PlayRunAnim.IsPlaying then
Humanoid.WalkSpeed = SprintSpeed
PlayRunAnim:Play(0.1)
end
end
end
end
end
end)
Everything else works just as intended, I just want to get around this error.
Video example of the animations bugging out:
Is there a way to make these issues stop happening?
Thanks
(P.S: there are also no errors)