I cannot play an animation, I am trying to make a player hold a gun with 2 hand when tool is equipeed. I know i have made a mistake but im not able to solve it, I would be really grateful for any help or advice.
i have looked thru many sites but to no help.
The ultimate goal is to loop the animation and play it when a tool is held.
here is the code:
local player = game.Players.LocalPlayer
local Character = player.Character
local Humanoid = Character:WaitForChild(“Humanoid”)
local Animator = Humanoid:WaitForChild(“Animator”)
local animation = Instance.new("Animation",script)
animation.AnimationId = "rbxassetid://6992692765"
local Anim = Animator:LoadAnimation(animation)
script.Parent.Ak.Equipped:Connect(function()
Anim.looped = true
Anim:Play()
end)
I think you can’t load animation to the Humanoid’s Animator, you have to load it in the Humanoid, just like this: Humanoid:LoadAnimation(animationInstance)
Are you the owner of those animations?
If not, for player safety reasons, ROBLOX doesn’t allow you to play other’s animations in your games, you can only play animations that you have made or animations that ROBLOX have made.
What value are the AnimationPriority’s for the animations set to?
They should be set to action, to override movement + idle or else it won’t be able to override the idle animation and it won’t appear as playing for you.
See more here: AnimationPriority
You should use :WaitForChild() instead of just getting the character instantly or else it can return nil since your character might have not loaded yet, I think this is the problem. Try to switch it to:
local Character = player:WaitForChild("Character")
local player = game.Players.LocalPlayer
local Character = player.Character
local Humanoid = Character:WaitForChild(“Humanoid”)
local animation = script.Parent.Animation
script.Parent.Ak.Equipped:Connect(function()
animation:Play()
animation:Loop = true
-- Item Equipped, Animation has been Playyed
else
script.Parent.Ak.Unequipped:Connect(function()
animation:Stop()
animation:Loop = false
-- Player Has Unequipped the item and the animation has stopped
end)