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
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.
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
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.
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
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)