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
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
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