Animations don't work after character is reset

So my animations for my gun work fine when I first join the game. however, if I reset my character, my animation declaration is for some reason nil, thus halting the entire gun script so that it doesn’t work anymore.

I get an error in the output saying: LoadAnimation requires the Humanoid Object

I’ve tried looking at solutions on the dev forum, such as this one: LoadAnimation requires the Humanoid Object

.
I’ve tried their solutions, but they do not work.

here is the client code:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mouse = game.Players.LocalPlayer:GetMouse()
local onCursorChange = script.Parent:WaitForChild("OnCursorChange")

local tool = script.Parent
local ammo = 10

while char.Parent == nil do
	char.AncestryChanged:wait()
end


local idleAnim = char:WaitForChild("Humanoid"):LoadAnimation(script.IdleAnim)
local shootAnim = char:WaitForChild("Humanoid"):LoadAnimation(script.ShootAnim)

print("Got past animation declaration")


tool.Equipped:Connect(function()
	
	
	idleAnim:Play()
	
end)

tool.Unequipped:Connect(function()

	
	idleAnim:Stop()
end)



tool.Activated:Connect(function()
	
	shootAnim:Play()
	ammo = ammo - 1
	script.Parent.Fire:FireServer(mouse.Hit.p)
	script.gunshot:Play()
	
	wait(1)
	print(ammo)
	if ammo <= 0 then
		reload()
	end

end)

function reload()
	script.Reload:Play()
	script.Disabled = true
	wait(4)
	ammo = 10
	script.Disabled = false

	
end

onCursorChange.OnClientEvent:Connect(function(mouseIcon)

	mouse.Icon = mouseIcon
end)



Let me know if you need anything else or if this is the wrong category.

2 Likes

Is this in the StarterPlayer or the StarterCharacter? Because if it’s in the StarterPlayer, then I believe when the player resets, the variable for the character does not work anymore. (I’m not an experienced scripter, so this might be wrong).

I have the tool currently in the starter pack.

1 Like

I searched around a bit, and try this: Animation error, humanoid has to be a descendant of the game object

(Loading the animations when the tool is equipped)

1 Like

Maybe try this instead:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()

local tool = script.Parent
local onCursorChange = tool:WaitForChild("OnCursorChange")

local ammo = 10
local shooting = false
local reloading = false

local function Reload()
    if not shooting and not reloading then
        reloading = true
        script.Reload:Play()
        
        wait(4)
        ammo = 10
        
        reloading = false
    end
end

local animator = humanoid:FindFirstChildOfClass("Animator")

if not animator then
    animator = Instance.new("Animator")
    animator.Parent = humanoid
end

local idleAnimation = animator:LoadAnimation(script.IdleAnim)
local shootAnimation = animator:LoadAnimation(script.ShootAnim)

tool.Equipped:Connect(function()
    idleAnimation:Play()
end)

tool.Unequipped:Connect(function()
    idleAnimation:Stop()
end)

tool.Activated:Connect(function()
    if not reloading then
        shooting = true

        ammo -=1
        script.Parent.Fire:FireServer(mouse.Hit.p)
        script.gunshot:Play()
        
        wait(1)
        shooting = false
        
        if ammo <= 0 then
            Reload()
        end
    end
end)

onCursorChange.OnClientEvent:Connect(function(mouseIcon)
    mouse.Icon = mouseIcon
end)
1 Like

Alright, I’ll be sure to try it out.

Sure, I’ll see if that will help.

I still get the same error: LoadAnimation requires the Humanoid object (CC_1204.Humanoid) to be a descendant of the game object

I tried out what you said, and in the output it says: Players.CC_1204.Backpack.Pistol.GunClient:46: attempt to index nil with ‘Play’

1 Like

We don’t know what is on line 46, please show.

1 Like

I have the same problem.seek help!

This error is saying : The humanoid needs to be the child of a model

try to print the animations , make sure they are being called for correctly