How can I make my running animation not play when standing still and holding shift after running

Hey, I’m kinda tired right now so I have no idea what i’m doing. Trying to get our games released by march so I’m trying to get all these useless bugs fixed if anyone can help me fix my running script not play after running once that would be cool. Video + Script below.

LocalScript:

local Services = {
	Players = game:GetService("Players"),
	RunService = game:GetService("RunService"),
	RS = game:GetService("ReplicatedStorage"),
	TS = game:GetService('TweenService'),
	CAS = game:GetService('ContextActionService')
}

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

local Camera = workspace.CurrentCamera

local Remote = Services.RS:WaitForChild("Event")

local animTrack = nil
local canSprint = true
local isSprinting = false

local sprintAnim = Instance.new("Animation")
sprintAnim.AnimationId = "rbxassetid://" .. 8755953869

local FOVIn = Services.TS:Create(
	Camera,
	TweenInfo.new(.5),
	{FieldOfView = 71}
)

local FOVOut = Services.TS:Create(
	Camera,
	TweenInfo.new(.5),
	{FieldOfView = 70}
)

local function handleSprintAction(actionName, inputState, inputObject)
	if actionName == "Sprint" and inputState == Enum.UserInputState.Begin and canSprint and Humanoid.MoveDirection.Magnitude > 0.01 then
		isSprinting = true

		animTrack = Humanoid:LoadAnimation(sprintAnim)
		animTrack:Play()

		Humanoid.WalkSpeed = 20.5
	elseif actionName == "Sprint" and inputState == Enum.UserInputState.End and canSprint then
		if animTrack ~= nil then
			animTrack:Stop()
			animTrack = nil
		end

		Humanoid.WalkSpeed = 16

		isSprinting = false
	end
end

Services.CAS:BindAction("Sprint", handleSprintAction, true, Enum.KeyCode.LeftShift)
Services.CAS:SetTitle("Sprint", "Sprint")
Services.CAS:SetPosition("Sprint",UDim2.new(1, -70, 0, 10))

Services.RunService.RenderStepped:Connect(function()
	if isSprinting then        
		FOVIn:Play()
	else
		FOVOut:Play()
		if not animTrack == nil then
			animTrack = Humanoid:LoadAnimation(sprintAnim)
			animTrack:Play()
			isSprinting = false
		end
	end
end)

Remote.OnClientEvent:Connect(function(val)
	if val == "CantMove" then
		Services.CAS:UnbindAction("sprint")
		canSprint = false

		if animTrack ~= nil then
			animTrack:Stop()
			animTrack = nil
		end
	elseif val == "CanMove" then
		Services.CAS:BindAction("Sprint", handleSprintAction, true, Enum.KeyCode.LeftShift)
		Services.CAS:SetTitle("Sprint", "Sprint")
		Services.CAS:SetPosition("Sprint",UDim2.new(1, -70, 0, 10))
		canSprint = true
	end
end)

Video:

1 Like

Use UIS to detect input ended. Create a variable “Shift” and set it to true whenever UIS, input began detects shift. If input ended and “Shift” equals true, make “Shift” false and stop the animation. This should work.

Can’t i just use magnitude 30char

Have you tried? I can’t see it being more simple tho

Well yea but it always fails some reason. And i have a Enum.Input.Begin and end and it doesnt do anything

local UIS = game:GetService("UserInputService")
local SPEED_ANIM = script.Parent.Anim
local Player = game:GetService("Players").LocalPlayer
local Running = false
local Track = nil

UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftShift or Key.KeyCode == Enum.KeyCode.RightShift then
		Track = Player.Character.Humanoid:LoadAnimation(SPEED_ANIM)
		Track:Play()
		Player.Character.Humanoid.WalkSpeed = 25
		Running = true
	end
end)

UIS.InputEnded:Connect(function(Key)
	if not Running or Track == nil then return end
	if Key.KeyCode == Enum.KeyCode.LeftShift or Key.KeyCode == Enum.KeyCode.RightShift or Player.Character.Humanoid.MoveDirection.Magnitude <= 0 then
		Track:Stop()
		Player.Character.Humanoid.WalkSpeed = 18
		Running = false
		Track = nil
	end
end)

I would use events to play the animation, but I just quickly came up with this. Let me know if it works.

1 Like
if (humanoid.MoveDirection).Magnitude <= 0 then
   -- your stopping code, this is an example
  animTrack:Stop()
  
end

Don’t forget that some people will still be holding shift although they stopped, this is why you need to be checking if they are running or not, if they arent running you stop the animation. By the way if I were you I would change the Walk In the Animate script and have it changed back to normal when the input is ended.

1 Like
if not animTrack == nil then

This is doing the reverse of what you want it to do. That’s the same as.

if animTrack then
local function moveDirectionChanged()
	if Humanoid.MoveDirection.Magnitude == 0 then
		animTrack:Stop()
	end
end

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged)
5 Likes

Does the same thing