Animation wont stop playing

i dont know why but everytime i call the stop animation function when i unequip a tool it just keeps the animation there

any ideas? im using a module script liek this one blow to handle all animations


local AnimationHandler = {}


local animations = {
	["PASTEL DE NATA"] = {
		R6 = "rbxassetid://id",
		R15 = "rbxassetid://id" 
	},
	["SARDINHA"] = {
		R6 = "rbxassetid://id", 
		R15 = "rbxassetid://id" 
	},
	["DB"] = {
		R6 = "rbxassetid://id", 
		R15 = "rbxassetid://id" 
	}
}


local sounds = {
	["PASTEL DE NATA"] = "rbxassetid://id", 
	["SARDINHA"] = "rbxassetid://id" 
}


local activeAnimations = {}


function AnimationHandler.PlayAnimation(player, toolName)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid then 
		warn("No humanoid found for player: " .. player.Name)
		return 
	end

	local animator = humanoid:FindFirstChildOfClass("Animator")
	if not animator then
		animator = Instance.new("Animator")
		animator.Parent = humanoid
	end

	local bodyType = humanoid.RigType.Name
	local animationId = animations[toolName] and animations[toolName][bodyType]
	if not animationId then 
		warn("No animation found for tool: " .. toolName .. " and bodyType: " .. bodyType)
		return 
	end

	
	local animation = Instance.new("Animation")
	animation.AnimationId = animationId
	local track = animator:LoadAnimation(animation)
	track:Play()


	activeAnimations[player.UserId] = track
	print("Playing animation for player: " .. player.Name .. " with tool: " .. toolName)
end


function AnimationHandler.StopAnimation(player)
	local track = activeAnimations[player.UserId]
	if track then
		track:Stop()  -- Stop the animation
		print("Stopped animation for player: " .. player.Name)
		activeAnimations[player.UserId] = nil 
	else
		warn("No active animation to stop for player: " .. player.Name)
	end
end



return AnimationHandler

Is the Animation Looped property set to true? I know it shouldn’t be a factor but still…
Are the print or warn statements showing up at all?

its not looped, but i keep getting the error “player not found for the unequiped tool” in the local script inside the tool

if player then
		local character = player.Character
		if character and character:IsA("Model") then
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			if humanoid then
				print("Humanoid found in character: " .. character.Name)
				print("Unequipped tool by player: " .. player.Name)  
				AnimationHandler.StopAnimation(player)
			else
				warn("Humanoid not found in character: " .. character.Name)
			end
		else
			warn("Character is not valid or is nil for player: " .. player.Name)
		end
	else
		warn("Player not found for unequipped tool.")
	end
end


What setsplayer in the second script?

What calls the AnimationHandler.StopAnimation function in the first script? If that isn’t firing then the function will never work.

Put a print in the first line of the StopAnimation function to print(player.UserID)

local player = Players:GetPlayerFromCharacter(tool.Parent.Parent) 

function AnimationHandler.StopAnimation(player)
	local track = activeAnimations[player.UserId]
	if track then
		track:Stop() 
		print("Stopped animation for player: " .. player.Name)
		activeAnimations[player.UserId] = nil 
	else
		warn("No active animation to stop for player: " .. player.Name)
	end
end

it doesnt print out anything

sssssssssssss

it was the player variable i changed it to game.Players.LocalPlayer

1 Like

If that was the Solution please mark it so others having the same issue can learn from your post. :sunglasses:

1 Like

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