Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[Solved] How do I make the animation not play when standing still?

I made a Run Animation script and a animation for it. The script works exactly how I want it except the script plays while the player isn't move. I don't want it too run while the player isn't moving. Is there a way I can do this?

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local sprinting
local humanoid = player.Character:WaitForChild("Humanoid")
local animationTrack = humanoid:LoadAnimation(game.StarterPlayer.StarterPlayerScripts.LocalScript.Animation)
	
UIS.InputBegan:Connect(function(input, procccesed, isTyping)
	if not procccesed then
		if isTyping then
			return 
		end
		if input.KeyCode == Enum.KeyCode.LeftShift then
			sprinting = not sprinting
			if sprinting then
				player.Character.Humanoid.WalkSpeed = 30
				animationTrack:Play()
			else 
				player.Character.Humanoid.WalkSpeed = 16
				animationTrack:Stop()
			end
		end
	end
end)

2 answers

✓ Accepted answer

Detect when the speed at which the humanoid is running changes with the Humanoid.Running event. If the speed is greater than 16, then play the track. Otherwise, stop the track.

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local sprinting
local humanoid = player.Character:WaitForChild("Humanoid")
local animationTrack = humanoid:LoadAnimation(game.StarterPlayer.StarterPlayerScripts.LocalScript.Animation)

UIS.InputBegan:Connect(function(input, procccesed, isTyping)
	if not procccesed then
		if isTyping then
			return 
		end
	if input.KeyCode == Enum.KeyCode.LeftShift then
		sprinting = not sprinting
			if sprinting then
				player.Character.Humanoid.WalkSpeed = 30
			else 
				player.Character.Humanoid.WalkSpeed = 16
			end
		end
	end
end)

humanoid.Running:Connect(function(speed)
	if speed > 16 then
		animationTrack:Play()
	else
		animationTrack:Stop()
	end
end)

if the humanoid state type changes to idle then it stops. (It's a humanoid property by the way)

This discussion is preserved as it was. New answers, comments, and votes are closed.