How can I stop an Animation if the Player walks

I want the animation (dances) to stop if I walk

You can stop it if you click on a button but i’d also like if people stopped dancing if they move

I have tried different animation priorities also in the script it is changeable but if you stop moving you see the character doing weird like he is doing the first millisecond of the animation

This is the script

local anims = script:WaitForChild("Animations"):GetChildren()

local templateBtn = script:WaitForChild("AnimButton")


local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local currentAnim = nil


for i, anim in pairs(anims) do
	
	
	local loadedAnim = humanoid:LoadAnimation(anim)
	loadedAnim.Looped = true
	loadedAnim.Priority = Enum.AnimationPriority.Action	
	
	
	local templateClone = templateBtn:Clone()
	templateClone.Text = anim.Name
	
	templateClone.Parent = script.Parent.AnimationsScroll
	
	
	templateClone.MouseButton1Click:Connect(function()
		
		if currentAnim ~= loadedAnim then 
			
			if currentAnim then currentAnim:Stop() end
			
			currentAnim = loadedAnim
			currentAnim:Play()
			
			
		elseif currentAnim == loadedAnim then
			
			currentAnim:Stop()
			currentAnim = nil
			
		end
	end)
end
2 Likes

https://developer.roblox.com/en-us/api-reference/property/Humanoid/MoveDirection

--animation
player.Character.Humanoid:GetPropertyChangedSignal ('MoveDirection'):Connect(stop)--

where stop is the function that stops it

1 Like

You can just change the animation priority from whatever it is now to Idle. You can find details on it here. Let me know if you need any help!

1 Like

You can use Humanoid.Running:

Humanoid.Running(function(Speed)
	if Speed >= 0.1 then
		Dance:Stop()
	end
end)

This checks if the player is moving at a walking or running speed. Since Humanoid.Running fires a lot more than it should.

1 Like

The 3 unique answers, I’ll say if not runningphysics then.

1 Like

id use this piece of code on a loop to check when a player is walking:

if humanoid.MoveDirection.Magnitude > 0 then
--code
1 Like

@SetOnRoblox has the solution:
Use

humanoid.Running:Connect(function(speed)
	if speed >= 0.1 then
		--code
	end
end)

to check when the player starts walking.


After that, :Stop the currentAnim AnimationTrack:

humanoid.Running:Connect(function(speed)
	if speed >= 0.1 then
		if currentAnim then
			currentAnim:Stop()
		end
	end
end)

Putting that all together:

local anims = script:WaitForChild("Animations"):GetChildren()

local templateBtn = script:WaitForChild("AnimButton")


local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local currentAnim = nil

local function setUpAnimation(anim)
	local loadedAnim = humanoid:LoadAnimation(anim)
	loadedAnim.Looped = true --Warning: might not replicate
	loadedAnim.Priority = Enum.AnimationPriority.Action
	
	local templateClone = templateBtn:Clone()
	templateClone.Text = anim.Name
	templateClone.Parent = script.Parent.AnimationsScroll

	templateClone.MouseButton1Click:Connect(function()
		if currentAnim ~= loadedAnim then 
			if currentAnim then currentAnim:Stop() end
			currentAnim = loadedAnim
			currentAnim:Play()
		elseif currentAnim == loadedAnim then
			currentAnim:Stop()
			currentAnim = nil
		end
	end)
end

humanoid.Running:Connect(function(speed)
	if speed >= 0.1 then
		if currentAnim then
			currentAnim:Stop()
		end
	end
end)

for i, anim in pairs(anims) do
	setUpAnimation(anim)
end

Don’t use loops when you can use events :+1:

1 Like
humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
	if humanoid.WalkSpeed > 0 then
		animationTrack:Stop()
	else
		animationTrack:Play()
	end
end)
2 Likes