Gun animation script problem

Making a third person shooting game and I ran into some trouble with my animations and playing them at the correct times. More specifically, the animations won’t play at all, including the idle animation and firing animation. This probably isn’t the best code (hence why it’s not working) since i’m new at scripting with Lua but if anybody could give me some support I would appreciate it

local UserInputService = game:GetService(“UserInputService”)
local tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild(“Humanoid”)

local AnimationIdle = Instance.new (“Animation”)
AnimationIdle.AnimationId = “rbxassetid://6324827560” – Idle animation ID
local IdleTrack = Humanoid:LoadAnimation(AnimationIdle)

local AnimationFiring = Instance.new(“Animation”)
AnimationFiring.AnimationId = “rbxassetid://6324711571” – Firing animation ID
local FiringTrack = Humanoid:LoadAnimation(AnimationFiring)

tool.Equipped:Connect(function()
     IdleTrack:Play() – Plays the idle animation on tool equip
end)

tool.Unequipped:Connect(function()
     if IdleTrack then
          IdleTrack:Stop() – Ends the idle animation on unequip
     end
end)

UserInputService.InputBegan:Connect(function(InputObject)
     if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
          if IdleTrack then
                IdleTrack:Stop() – Ends the idle animation on firing animation
          end
          FiringTrack:Play() – Plays the firing animation
     end
end)
1 Like

the priority on the animations need to be on movement or above else they’d get overwritten by the default animations

I have the idle animation set as movement and the firing animation set as action. It still shows the tool as the regular idle position and it won’t play the firing animation when the mouse is clicked.

try setting the animations’ priorities manually and see if that helps

IdleTrack.Priority = Enum.AnimationPriority.Action
FiringTrack.Priority = Enum.AnimationPriority.Action

if that doesn’t work try adding a print to the equipped connection to see if it’s starting at all

I’m getting the error “attempt to index nil with ‘WaitForChild’” so I added a CharacterAdded:Wait() to wait for the character to load in and it seems like it’s still resolving as nil, which is odd. I manually set the track priority as well so I don’t assume that is the issue anymore.

try changing

local Character = Player.Character

to

local Character = Player.Character or Player.CharacterAdded:Wait()

Looks like setting it to Player.CharacterAdded:Wait() worked. Thanks for the help!