Tool stops working after reset/death

Hey! Im still rather new to scripting so I can guess this is a rookie mistake. I’ve googled around for a bit and couldn’t find exactly what I was looking for so maybe someone here knows :slight_smile:

I had a simple flashlight tool that when equipped it plays an animation. When activated it turns on and plays a sound. When either deactivated or unequipped it will essentially “Turn off”.

My issue is simple. When my character dies or resets. the animation stops working and the tool just stops. Clicking it on and off doesn’t work and all I’m left with is the character holding out their arm with an unresponsive flashlight.

The error code is:

LoadAnimation requires the Humanoid object (MyCharactername.Humanoid) to be a descendant of the game object

so it has to do with something regarding the animation?

Code:

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local h = char:WaitForChild("Humanoid")
local light = script.Parent.Handle.Part.Attachment.SpotLight
local Mat = script.Parent.Handle.Part
local animation = h:LoadAnimation(script.Parent:WaitForChild("Idle"))
local Light = script.Parent.Handle.Part.Attachment.SpotLight
local sound = script.Parent.Sound

local tool = script.Parent

tool.Equipped:Connect(function()
	print("Equiped")
	animation:Play()
end)

tool.Unequipped:Connect(function()
	print("Unequiped")
	light.Range = 0
	Mat.Material = Enum.Material.Glass
	animation:Stop()
end)

tool.Activated:Connect(function()
	if light.Range == 0 then
		light.Range = 60
		sound:Play()
		Mat.Material = Enum.Material.Neon
	else
		sound:Play()
		light.Range = 0
		Mat.Material = Enum.Material.Glass
	end
end)

edit: formatted the code, my apologies!
Any advice (or ridicule) is appreciated xD I’m sure this is just me being silly lol.

format format format format please.

1 Like

Load animation is deprecated: Humanoid | Documentation - Roblox Creator Hub

Also, please format your code using ``` as it’s very hard to read your code.

1 Like

Yes I realized my mistake after i posted it, sorry! normally It formats itself. Thank you!

When a player respawns, they’re given a new character, humanoid, etc. and the old character and humanoid are deleted and old references to them are also deleted. You would need to re-reference the character and humanoid when your character loads. You can do this with Player.CharacterAdded.

If you want, you can put the tool in starterpack, that way the tool is given to the player each time the player resets

1 Like

Oh thank you! and yes, the tool is within the starterpack.

If the tool is already inside starterpack and the script is inside the tool then the script you first had should work, not sure why it doesn’t

1 Like

Yeah, I know, It’s weird. Someone above mentioned about character adding but if the item is already in starter pack then there shouldn’t be an issue.

I’ve disabled both the sound and the animation (because when I disable the animation, the sound became the issue) it works. Something with the sound and animation break it.

You’re trying to load an animation whilst the humanoid isn’t loaded in yet

a simple fix for you is to throw a wait(1) at the top of the script

1 Like

how about wrapping all the equip/unequip events inside player.CharacterAdded?

local player = game:GetService("Players").LocalPlayer
local tool = script.Parent
local handle = tool.Handle
local Mat = handle.Part
local light = Mat.Attachment.SpotLight
local sound = tool.Sound
local idle = tool.Idle
player.CharacterAdded:Connect(function(char) -- char is the character when its loaded
	local h = char:WaitForChild("Humanoid")
	local animation = h:LoadAnimation(idle)

	tool.Equipped:Connect(function()
		print("Equiped")
		animation:Play()
	end)

	tool.Unequipped:Connect(function()
		print("Unequiped")
		light.Range = 0
		Mat.Material = Enum.Material.Glass
		animation:Stop()
	end)

	tool.Activated:Connect(function()
		if light.Range == 0 then
			light.Range = 60
			sound:Play()
			Mat.Material = Enum.Material.Neon
		else
			sound:Play()
			light.Range = 0
			Mat.Material = Enum.Material.Glass
		end
	end)
end)

note that when the tool equips/unequips the sound wont play because the character hasnt loaded in

1 Like

Hey! I was trying to figure this out myself for a while. Here is what you have to do.

local player = game:GetService("Players").LocalPlayer
local char = player.CharacterAdded:Wait()
local h = char:WaitForChild("Humanoid")
local light = script.Parent.Handle.Part.Attachment.SpotLight
local Mat = script.Parent.Handle.Part
local animation = h:LoadAnimation(script.Parent:WaitForChild("Idle"))
local Light = script.Parent.Handle.Part.Attachment.SpotLight
local sound = script.Parent.Sound

local tool = script.Parent

tool.Equipped:Connect(function()
if char then
	print("Equiped")
	animation:Play()
      end
end)

tool.Unequipped:Connect(function()
if char then
	print("Unequiped")
	light.Range = 0
	Mat.Material = Enum.Material.Glass
	animation:Stop()
     end
end)

tool.Activated:Connect(function()
if char then
	if light.Range == 0 then
		light.Range = 60
		sound:Play()
		Mat.Material = Enum.Material.Neon
	else
		sound:Play()
		light.Range = 0
		Mat.Material = Enum.Material.Glass
	end
end
end)
2 Likes