Hello everyone! Recently, I wrote some code: when you click a button, an animation should play, but it doesn’t work. I can’t find the mistake. I need help!
local gui = script.Parent.Parent
local button = gui.anim.TextButton
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local hitAnim = Instance.new("Animation")
hitAnim.AnimationId = "rbxassetid://71954684660095"
local hitTrack = humanoid:LoadAnimation(hitAnim)
button.MouseButton1Click:Connect(function()
hitTrack:Play()
print("Anim is started!")
end)
local button = script.Parent.TextButton
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
button.MouseButton1Click:Connect(function()
local s, er = pcall(function()
local hitAnim = Instance.new("Animation")
hitAnim.AnimationId = "rbxassetid://71954684660095"
local hitTrack = humanoid:LoadAnimation(hitAnim)
hitTrack:Play()
end)
if s then print("Anim is started!") elseif not s then warn(er) end
end)
You are missing the animator part. Below is an example, make sure you load your animation into the animator of the Humanoid.
-- Create a new "Animation" instance and assign an animation asset ID
local kickAnimation = Instance.new("Animation")
kickAnimation.AnimationId = "rbxassetid://2515090838"
-- Load the animation onto the animator
local kickAnimationTrack = animator:LoadAnimation(kickAnimation)