Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

When the player dies, my simple tool breaks. What?

Asked by 6 years ago

Hello,

I'm creating a simple gun tool. It's pretty simple, all it does is load animations, play sounds, and deal damage.

It's working fine, but when the player dies and respawns, the gun stops working. Here is the error,

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

And here is the script.

Client Side Script:

-- Made by Chardinok if you somehow find this no steal por favor

-- Variables
local tool = script.Parent
local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local chr = plr.Character or plr.CharacterAdded:wait()
local hum = chr:WaitForChild("Humanoid")
local shootevent = tool:WaitForChild("shootevent")
local animating = false

-- Animations
local shootanim = Instance.new("Animation")
shootanim.AnimationId = "rbxassetid://2208064116"
local idleanim = Instance.new("Animation")
idleanim.AnimationId = "rbxassetid://2205688680"
local idletrack
local shoottrack

-- Functions
tool.Equipped:connect(function()
    idletrack = hum:LoadAnimation(idleanim)
    idletrack:Play()
end)

tool.Activated:connect(function()
    if animating == false then
        animating = true
        local mpos = mouse.Hit.p
        shoottrack = hum:LoadAnimation(shootanim)
        shoottrack:Play()
        wait(0.05)
        shootevent:FireServer(mpos)
        wait(0.05)
        shoottrack:Stop()
        animating = false
    end
end)

tool.Unequipped:connect(function()
    idletrack:Stop()
end)

Server Side Script:

-- Made by Chardinok if you somehow find this no steal por favor

-- Variables
local tool = script.Parent
local plr = tool.Parent.Parent
local chr = plr.Character or plr.CharacterAdded:wait()
local hum = chr:WaitForChild("Humanoid")
local shootevent = tool.shootevent
local damagepart = tool.damagepart

-- Sounds
local equipsound = tool.Handle.equip
local shootsound = tool.Handle.gunshot

-- Functions
tool.Equipped:connect(function()
    equipsound:Play()
end)

shootevent.OnServerEvent:connect(function(player, mpos)
    shootsound:Play()
    local bullet = damagepart:clone()
    bullet.Position = mpos
    bullet.damage.Disabled = false
    bullet.Parent = game.Workspace
end)

Every response is greatly appreciated.

0
Try adding a wait or WaitForChild to wait for the Humanoid to exist before running code on it? hipenguinflip 30 — 6y

Answer this question