Animation Can Only Play Once?

So, I have this Code that plays an animation based on the mouse direction. however once an animation is played once it seems to be stuck on rig and wont play again

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local tool = script.Parent

local Animations = {
	Up = Humanoid:LoadAnimation(script.Animations.Up),
	Down = Humanoid:LoadAnimation(script.Animations.Down),
	Left = Humanoid:LoadAnimation(script.Animations.Left),
	Right = Humanoid:LoadAnimation(script.Animations.Right)
}

local function onActivated()
	local startPosX, startPosY = Mouse.X, Mouse.Y
	local threshold = 500

	local function playAndReset(animation)
		animation.Priority = Enum.AnimationPriority.Movement
		animation.Looped = false
		animation:Play()
		animation.Stopped:Connect(function()
			animation:Stop()
		end)
	end

	tool.Activated:Connect(function()
		startPosX, startPosY = Mouse.X, Mouse.Y
	end)

	tool.Deactivated:Connect(function()
		local endPosX, endPosY = Mouse.X, Mouse.Y
		local deltaX, deltaY = endPosX - startPosX, endPosY - startPosY

		if math.abs(deltaX) > math.abs(deltaY) then
			-- Horizontal movement is greater
			if deltaX > 0 then
				-- Mouse moved to the right
				playAndReset(Animations.Right)
				warn(1)
			else
				-- Mouse moved to the left
				playAndReset(Animations.Left)
				warn(2)
			end
		else
			-- Vertical movement is greater
			if deltaY > 0 then
				-- Mouse moved down
				playAndReset(Animations.Down)
				warn(3)
			else
				-- Mouse moved up
				playAndReset(Animations.Up)
				warn(4)
			end
		end
	end)
end

tool.Activated:Connect(onActivated)

1 Like

Try changing priority to Enum.AnimationPriority.Action, other character animations may be getting in the way of it.

1 Like

Ive Tried this and it doesnt help

let me add a video to the Original Post

uhhh… i just read into your code, is it really necesarry to connect the functions inside the onActivated?
change the tool.Deactivated:Connect(function() into tool.Deactivated:Once(function(), this might help a bit.

because you are connecting to the deactivation every time you activate (click) your tool, that’s not very good

so thats whats causing the errors? I see ill look into this and let you know

welp, your the amount of prints is rising, that’s because the connections are stacking
image

Im still having the Issue where my animations are playing only once here is the video.
the firing stopped but did not fix my issue

that seems to be fixed now but im still having the animation Bug.

Try to use this in playAndReset function, instead of the animation.Stopped:Connect:

task.delay(2, function()
    animation:Stop()
end)

Just for debugging, so if this fixes the issue, we know this was the problem.

alright ill try this and let you know

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.