Hi there, I’m making a tool to play an animation on tool activation, how should I do that? I tried using Humanoid:LoadAnimation, it worked in studio, not in the actual game.
Script;
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local anim = script.Animation
script.Parent.Activated:Connect(function()
local track = char.Humanoid:LoadAnimation(anim)
track:Play()
script.Parent.Bites.Value = script.Parent.Bites.Value + 1
if script.Parent.Bites.Value >= 6 then
script.Parent:Destroy()
end
end)
end)
end)
I need a reference to the player’s character somehow for this to work. Since it’s a ServerScript I can’t get it from the LocalPlayer, which is why the CharacterAdded and PlayerAdded events was used.
Don’t use Humanoid:LoadAnimation(), it’s deprecated. Instead, there’s something called an Animator under the player character’s Humanoid, use that to load animation.
Calling the CharacterAdded event is not the right way to get the player’s character for a script that is inside a tool. Since Tool.Activated runs when the player clicks/taps while their weapon is equipped, you can easily get the character because whichever tool you equip, the tool goes inside the character.
Here is your fixed code:
local Tool = script.Parent
local Bites = Tool:WaitForChild("Bites")
local anim = script:WaitForChild("Animation")
Tool.Activated:Connect(function()
local char = Tool.Parent
local hum = char:WaitForChild("Humanoid")
local track = hum.Animator:LoadAnimation(anim)
track:Play()
Bites.Value += 1
if Bites.Value >= 6 then
Tool:Destroy()
end
end)