My gun animation script doesn't work

local plr = game.Players.LocalPlayer

plr.CharacterAdded:Connect(function(char)
local hum = char:FindFirstChild('Humanoid')
local holster = hum:LoadAnimation(script:WaitForChild('Holster')
local walk = hum:LoadAnimation(script:WaitForChild('Walk')

script.Parent.Equipped:Connect(function()
	holster:Play()
	holster.Looped = true
end)

script.Parent.Unequipped:Connect(function()
 	 if (holster) then
	 	 holster:Stop()
		 holster.Looped = false
end
end)
end)

I’m trying to write a script that triggers an animation to play and then stop after being equipped/unequipped. But it says this as an error.

Players.vf9r.Backpack.Kolibri.Animate:6: Expected ')' (to close '(' at line 5), got 'local'

Okay I’ll try that. I hope it works.

Okay, another error happened after that.

17:34:22.007 - Players.vf9r.Backpack.Kolibri.Animate:5: attempt to index nil with 'LoadAnimation'
17:34:22.008 - Stack Begin
17:34:22.009 - Script 'Players.vf9r.Backpack.Kolibri.Animate', Line 5
17:34:22.009 - Stack End

That’s because the humanoid is nil, in other words, it didn’t load in when you set the variable. Instead of doing char:FindFirstChild('Humanoid'), write char:WaitForChild('Humanoid') so that it waits until the humanoid is loaded.

Next time, whenever there are output errors, go the line that it says. In the output you provided, it said line 5, so I immediately went to line 5 and caught the error. Also, search the Forum for other posts and the DevHub before posting a new topic. Maybe the bug is very simple and it shows in the output.

1 Like

Use :WaitForChild() instead of Dot Syntax, whereas less efficient it has chances of preventing nil , specify the timeOut parameter to prevent thread yielding indefnitely.

1 Like

Thanks! @TheCarbyneUniverse You also helped, thank you too! :slight_smile:

2 Likes