The animations doesn't work after reset

Hi, I’m Axel, I’ve been working with the animations for my game, but there is a bug that broke the animations and it no longer works. The animations work perfectly when I enter the place but when I rebset or die I get this error:
Error

This is my local script that is inside a tool in Starter Pack:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local tool = script.Parent
local StrenghAnimation = script:WaitForChild("StrenghAnimation")
local Anim = Humanoid:LoadAnimation(StrenghAnimation)
local StrenghAnimation2 = script:WaitForChild("StrenghAnimation2")
local Anim2 = Humanoid:LoadAnimation(StrenghAnimation2)
local RS = game:GetService("ReplicatedStorage")
local Strengh = RS.RemoteEvents.Strengh
local db = false

--Animation
tool.Equipped:Connect(function()
	Anim:Play()
	Anim.Priority = Enum.AnimationPriority.Idle
	Humanoid.UseJumpPower = true
	Humanoid.WalkSpeed = 0
	Humanoid.JumpPower = 0
end)
tool.Unequipped:Connect(function()
	Anim:Stop()
	Anim2:Stop()
	Humanoid.UseJumpPower = true
	Humanoid.WalkSpeed = 16
	Humanoid.JumpPower = 50
end)
tool.Activated:Connect(function()
	if db == false then
		db = true
		Anim2:Play()
		Anim2.Priority = Enum.AnimationPriority.Action
		Strengh:FireServer()
		wait(2)
		db = false
	end
end)

I have tried everything I know to fix it, but I don’t know how and I don’t know why this error appears after reset or die.

I also forgot that, I want the animations to be played on the server since I can only see it in local player, but I have not been able to do it either and I do not want to break the animations.

1 Like

you should get the character and the rest of the character’s parts by getting the tool’s Parent, i did a similar thing and it broke when i died or reset and spawned back because of something i guess (idk)

basically, you have to write a code line in one of the tool equipped thing, make sure to also include the unequipped inside it so it can still have access to the vars without trying to transport it, you get the parent of the Tool when it gets equipped, you also get the Humanoid, anything else you may need, you also include the activated function in the equipped function thing but make it in the higher hierarchy than tool.unequipped thing i guess due to perfectionism problems

the first 5 codelines of the function should look like this

tool.Equipped:Connect(function()
    local char = tool.Parent
    local hum = char:FindFirstChild("Humanoid")
    if not char or hum or hum.Health > 0 then return end
	Anim:Play()
	Anim.Priority = Enum.AnimationPriority.Idle
	Humanoid.UseJumpPower = true
	Humanoid.WalkSpeed = 0
	Humanoid.JumpPower = 0
end)

That happens because the character is destroyed every time you die, so when you try to reference the humanoid for the animation to play again it doesn’t find it and breaks. To fix it you can either move the script to StarterCharacterScripts so the script resets too, or you can do something like this:

Player.CharacterAdded:Connect(function(Char) --When the character respawns
Character = Char --Update Char variable
Humanoid = Character:WaitForChild("Humanoid") --Update Humanoid variable
end)
1 Like

I did what you asked me, but there was a problem, and that is that when trying to test it, the animation does not play and when I die it keeps giving me the same error, the animations work when I enter and there is no problem but everything stops working when I restart, and when I click on the error, it refers to this line of code:

local Anim = Humanoid:LoadAnimation(StrenghAnimation)

you have to write that inside the tool equipped function too, when the animation ends don’t forget to destroy it so you don’t atleast fill 1 inside the maximum animation track limits

also, can you write your code in here? so it’s easier what you did so i can identify the problem easier

just copy the code block into the equipped event:

tool.Equipped:Connect(function()
	local char = tool.Parent
	local hum = char:FindFirstChild("Humanoid")
	if not char or hum or hum.Health > 0 then return end
	Anim:Play()
	Anim.Priority = Enum.AnimationPriority.Idle
	Humanoid.UseJumpPower = true
	Humanoid.WalkSpeed = 0
	Humanoid.JumpPower = 0
end)
tool.Unequipped:Connect(function()
	Anim:Stop()
	Anim2:Stop()
	Humanoid.UseJumpPower = true
	Humanoid.WalkSpeed = 16
	Humanoid.JumpPower = 50
end)
tool.Activated:Connect(function()
	if db == false then
		db = true
		Anim2:Play()
		Anim2.Priority = Enum.AnimationPriority.Action
		Strengh:FireServer()
		wait(2)
		db = false
	end
end)

sorry if I have done something wrong, I do not have much experience with this.

yeah don’t be sorry it’s just letters B)

also, you have to also include the Anim player var inside the equipped function as well, if you load the humanoid outside the tool equipped it wouldn’t work in the first place since Humanoid can’t be safely received

anyways, like the other vars you place it inside the same function, you also have to insert the other tool functions inside the equipped too like i was saying before, so it can access the variables without having to use vessels

here’s a clear example of what i meant, hopefully it solves the problem

tool.Equipped:Connect(function()
	local char = tool.Parent
	local hum = char:FindFirstChild("Humanoid")
	if not char or hum or hum.Health > 0 then return end
    local Anim = Humanoid:LoadAnimation(StrenghAnimation)
    local Anim2 = Humanoid:LoadAnimation(StrenghAnimation2)
	Anim:Play()
	Anim.Priority = Enum.AnimationPriority.Idle
	Humanoid.UseJumpPower = true
	Humanoid.WalkSpeed = 0
	Humanoid.JumpPower = 0
	tool.Activated:Connect(function()
		if db == false then
			db = true
			Anim2:Play()
			Anim2.Priority = Enum.AnimationPriority.Action
			Strengh:FireServer()
			wait(2)
			db = false
		end
	end)
    tool.Unequipped:Connect(function()
		Anim:Stop()
		Anim2:Stop()
        Anim:Destroy()
        Anim2:Destroy()
		Humanoid.UseJumpPower = true
		Humanoid.WalkSpeed = 16
		Humanoid.JumpPower = 50
	end)
end)

It’s worth nothing that Humanoid:LoadAnimation is deprecated, please refer to Humanoid.Animator. Animator | Roblox Creator Documentation

I still have the same error and it is that this code block interrupts the animation of the equipped event:

local char = tool.Parent
	local hum = char:FindFirstChild("Humanoid")
	if not char or hum or hum.Health > 0 then return end

Besides that he keeps referring to this:

I also want to make the animations play on the server since even if the animations work, only the local player will be able to see them and not the entire server.

In fact, the animations work, they only break when the character reset or dies.

Try loading the animation once the tool is equipped, seems like it only loads the animation once for every player.

by the way almost forgot, back then humanoids were used to mainly load animations and play it in them, they also replicated to the server so what do you think most knowledgeable developers use now that using the humanoid is deprecated?

well, it’s the “Animator” instance inside the Humanoid, it basically is like the original Humanoid, it can replicate local animations for the server to see, i don’t recommend playing animations in normal scripts since it can be delayed

also, a plausible issue that you should remember is if the Humanoid somehow doesn’t have an Animator in it, don’t create the animator instance in it from a local script, it’s basically an illusion from the player and it’ll error.

to use Animator, you simply write local anim = Humanoid:FindFirstChild("Animator"), there are probably no common ways where your humanoid doesn’t have an animator since the default animate script is required to use it, so basically you create another line but you don’t delete the Hum line, you replace some of the used abbrevs in the anim lines

local Animator = Humanoid:FindFirstChild("Animator")
local Anim = Animator:LoadAnimation(StrenghAnimation)
local Anim2 = Animator:LoadAnimation(StrenghAnimation2)

also you’re supposed to write the functions inside the equipped function αααα

tool.Equipped:Connect(function()
	local char = tool.Parent
	local hum = char:FindFirstChild("Humanoid")
	if not char or hum or hum.Health > 0 then return end
    local Anim = Humanoid:LoadAnimation(StrenghAnimation)
    local Anim2 = Humanoid:LoadAnimation(StrenghAnimation2)
	Anim:Play()
	Anim.Priority = Enum.AnimationPriority.Idle
	Humanoid.UseJumpPower = true
	Humanoid.WalkSpeed = 0
	Humanoid.JumpPower = 0

	tool.Activated:Connect(function()
		if db == false then
			db = true
			Anim2:Play()
			Anim2.Priority = Enum.AnimationPriority.Action
			Strengh:FireServer()
			wait(2)
			db = false
		end
	end)

    tool.Unequipped:Connect(function()
		Anim:Stop()
		Anim2:Stop()
        Anim:Destroy()
        Anim2:Destroy()
		Humanoid.UseJumpPower = true
		Humanoid.WalkSpeed = 16
		Humanoid.JumpPower = 50
	end)

end)

Simple equip function that plays any animation:

local Tool = script.Parent

local function Equipped()
	
	local Character = Tool.Parent
	
	if Character and Character:FindFirstChildOfClass("Humanoid") then
		
		local Humanoid = Character.Humanoid
		
		local Animator = Humanoid.Animator
		
		local Animation = Instance.new("Animation")
		
		Animation.AnimationId = "rbxassetid://" --AnimationId here
		
		local AnimationTrack = Animator:LoadAnimation(Animation)
		
		AnimationTrack:Play()
	end
end

Tool.Equipped:Connect(Equipped)

I don’t know if your game is R6 or R15, however if you want a R6 animation you should animate it with a R6 rig. Same rule applies to R15 animations.

Something weird happened, and that is that when I tried to test the game, the animations were reproduced and everything worked fine, but when I died I got this error now:
Error3

This is how the script was:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local tool = script.Parent
local Animator = Humanoid:FindFirstChild("Animator")
local Anim = Animator:LoadAnimation(script.StrenghAnimation)
local Anim2 = Animator:LoadAnimation(script.StrenghAnimation2)
local RS = game:GetService("ReplicatedStorage")
local Strengh = RS.RemoteEvents.Strengh
local db = false

--Animation
tool.Equipped:Connect(function()
	Anim:Play()
	Anim.Priority = Enum.AnimationPriority.Idle
	Humanoid.UseJumpPower = true
	Humanoid.WalkSpeed = 0
	Humanoid.JumpPower = 0
end)
tool.Activated:Connect(function()
	if db == false then
		db = true
		Anim2:Play()
		Anim2.Priority = Enum.AnimationPriority.Action
		Strengh:FireServer()
		wait(2)
		db = false
	end
end)
tool.Unequipped:Connect(function()
	Anim:Stop()
	Anim2:Stop()
	Anim:Destroy()
	Anim2:Destroy()
	Humanoid.UseJumpPower = true
	Humanoid.WalkSpeed = 16
	Humanoid.JumpPower = 50
end)

refer to this now:

local Anim = Animator:LoadAnimation(script.StrenghAnimation)

In fact, everything worked fine and I no longer get the error from before, but I get another error:
Error3

That means your ‘‘StrengthAnimation’’ is nil, it does not exist.

You could manually create an animation by instance instead of relying on a child.

		local Animation = Instance.new("Animation")
		
		Animation.AnimationId = "rbxassetid://"
		
		local AnimationTrack = Animator:LoadAnimation(Animation)
		
		AnimationTrack:Play()

Instead of:

		local Animation = script.Animation
		
		local AnimationTrack = Animator:LoadAnimation(Animation)
		
		AnimationTrack:Play()

Both options work, it’s just that the first one is more reliable and you won’t face the non-existence of the child.

I’m a bit tired trying to fix this, apparently I’m facing another problem:
image

That means you are checking the backpack for a Humanoid, could you send me the current code? I will try and fix it for you.