So I’ve made a toggle to sprint system (when you press left shift your walkspeed stays the same until you press it again). However, the issue I’m running into is, whenever the player character stops moving, the animation keeps playing. I’ve tried using MoveDirection, but I’m very unfamiliar with it so my attempts haven’t really been workin out.
Video of the issue
And here’s the script for it
-- local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://17163538419'
local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
local RunService = game:GetService("RunService")
local Humanoid = Character:WaitForChild("Humanoid")
local Running = false
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Running = not Running
if Running == true then
if Humanoid.MoveDirection.Magnitude > 0 then
script.Parent.basicMoves.dashingScript.Enabled = true
Character.Humanoid.WalkSpeed = 12 --speed
PlayAnim:Play()
print("GET OUT THE WAY!!!!")
else
script.Parent.basicMoves.dashingScript.Enabled = false
Character.Humanoid.WalkSpeed = 6 --speed
PlayAnim:Stop()
print("Oh no")
end
end
end
end)
local UIS = game:GetService("UserInputService")
UIS.InputEnded:Connect(function(input, chatting)
if chatting then return end
if input.KeyCode == Enum.KeyCode.W then
print("Stopped Moving")
end
end)
Using the UIS.InputEnded, it checks if the key is released.
local UIS = game:GetService("UserInputService")
UIS.InputEnded:Connect(function(input, chatting) -- Check if the key is released
if chatting then return end
if input.KeyCode == Enum.KeyCode.LeftShift then -- Check if the sprint key is released
print("removed sprinting key")
end
end)
If you want to check if the player stopped moving, then you should use a loop
local RS = game:GetService("RunService")
RS.Stepped:Connect(function()
if Humanoid.MoveDirection.Magnitude <= 0 then
print("Player Stopped Moving")
end
end)
Instead of trying to use a loop, you COULD InputEnded as mentioned before. I wouldn’t use MoveDirection to see if a player is moving. I would use humanoidRoot.AssemblyLinearVelocity. MoveDirection is a normalized directional vector, its not really intended to be used in that fashon.
Moreover, this is a classic X/Y problem. If I understand correctly, your goal is to stop the run animation when the player stops running, not when the player stops moving. There is no need for any loops or connections to game processes, thats all just overhead. Eliminate the entire loop, and try something along these lines
I have switched to context action service as this is a reoccurring action associated with one specific key.
local running = false
local function updateRun(_,state,_)
if state==Enum.UserInputState.Begin then
running = not running
--read running bool accordingly here, and apply any changes you would like to make.
end
end
ContextActionService:BindAction("ToggleSprint",true,updateRun,Enum.KeyCode.LeftShift)
Another solution to your problem is taking the speed of the player directly from the humanoid. and just checking how far the humanoids position changes. you can do this using the humanoid.Running Event. In the end it should look like this.
local Running = true
Humanoid.Running:Connect(function(Speed)
if Speed > 0 then
Running = true
else
Running = false
end
end)
if you’re running happens to be faster than a walking speed just check if its below that speed instead of 0.
Thank you for the solution but there’sa few issues with it. For example, f I hit shift while standing still, the animation continues to play anyway, and if I stop running while still moving the animation will continue to play until my character has fully stopped.
Thank you, this is prolly the best solution so far, but I’m running into an issue where if I use a loop to determine when to stop and restart the animation, it just ends up restarting the animation every time it loops, which just ends up breaking it.