How can I fix my running animation debounces?

I have a left shift prompted running script here and I would like the character to play the running animation when shift is toggled and stop when released, however I’m having trouble with stopping the animation when shift is released.
Here’s the local script:

local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		ReplicatedStorage.RemoteEvents.Sprint:FireServer("StartedState")
	end
end)

UIS.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		ReplicatedStorage.RemoteEvents.Sprint:FireServer("EndedState")
	end
end)

ReplicatedStorage.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(Stamina,MaxStamina)
	Players.LocalPlayer.PlayerGui.Stamina.StaminaBar.Size = UDim2.new(0.1,(Stamina/MaxStamina) * 410,0,31)
	
end)

Here’s the part of the script:

ReplicatedStorage.RemoteEvents.Sprint.OnServerEvent:Connect(function(Player,State)
	local Humanoid = Player.Character.Humanoid
	local RunAnimation = Instance.new("Animation")
	RunAnimation.AnimationId = 'rbxassetid://6059811537'
	local LoadRunAnimation = Humanoid:LoadAnimation(RunAnimation)
	LoadRunAnimation.Priority = Enum.AnimationPriority.Movement
	if State == "StartedState" and Humanoid:GetState() == Enum.HumanoidStateType.RunningNoPhysics and Humanoid.MoveDirection.Magnitude > 0 then
		SprintingPlayers[Player.Name] = Humanoid.WalkSpeed
		Humanoid.WalkSpeed = Humanoid.WalkSpeed * SprintModifier
		if SprintingPlayers[Player.Name] then
		 LoadRunAnimation:Play()
		end
		elseif
		State == "EndedState" and SprintingPlayers[Player.Name] then
		Humanoid.WalkSpeed = SprintingPlayers[Player.Name]
		SprintingPlayers[Player.Name] = nil
	end
	if SprintingPlayers[Player.Name] == nil then
		LoadRunAnimation:Destroy()
	end
end)

RunService.Heartbeat:Connect(function()
	for i,Player in pairs(Players:GetChildren()) do
		local Stamina = Player.Stamina
		local Name = Player.Name
		
		if not SprintingPlayers[Name] then
			if Stamina.Value > MaxStamina then
				Stamina.Value = MaxStamina
			elseif
				Stamina.Value < MaxStamina then
				Stamina.Value = Stamina.Value + StaminaRegeneration
			
			end
		else
			if Stamina.Value >= SprintStaminaCost then
				Stamina.Value = Stamina.Value - SprintStaminaCost
				
			else
				Player.Character.Humanoid.WalkSpeed = SprintingPlayers[Name]
				SprintingPlayers[Name] = nil
			end
		end
	end
end)

I’ve tried placing debounces, more conditional statements but none worked. Should I even play the animation in this function of the script or in the heartbeat function that runs later down the script? I understand that Humanoid:LoadAnimation has been deprecated, but I doubt that alone is the root of my issue.

The load animation you reference in the state ended chunk isn’t the running animation of the player. You’d probably be better off doing the animation on the client so you can keep a reference to the animation more easily when it’s created.

That way other players won’t be able to see the animation then? I’m not quite sure I follow you.

Which part is the running animation of the player then?

Animations are exempt from the client server boundary, they will be replicated automatically. The default animation script is a localscript if you wanna see some evidence of this.

You could use humanoid:GetPlayingAnimationTracks() to properly reference the animation on the server I think.

Need I reference it server side if it replicates by default?

If you use the client to run the animations then no you can just keep a reference to the track when you make the track.

If you want to use the server then you’d use something like :GetPlayingAnimationTracks().

You don’t need to run animations from the server side, but you can if you want to. You just need to make sure that the animator instance that is created within the humanoid is loaded before you try and run animations on the client.

I see, thanks a lot for the help.

1 Like