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

How can I fix this Death script?

Asked by
Zerio920 285 Moderation Voter
10 years ago

This localscript, located in the player itself, is supposed to activate when the player dies (by falling off the map usually)

local plr = script.Parent

local char = plr.Character
local hum = char.Humanoid
local stats = plr.leaderstats
local respawns = stats.Respawns

local function onDeath()
if hum.Health <= 0 then
if respawns.Value > 0 then
respawns.Value = respawns.Value - 1
for i,v in pairs(plr:GetChildren()) do if v:IsA("Sound") then repeat wait(0) v:Stop() until v:Stop() or not v.IsPlaying end end
plr.Backpack.Death:Play()
end
end
end

hum.Changed:connect(onDeath)

Doesn't seem to work at all however, and I'm not finding any errors in the output. Any help would be appreciated.

1 answer

Log in to vote
1
Answered by
Muoshuu 580 Moderation Voter
10 years ago

Put this in a script in the StarterGui (Regular or Local)

local Player=script.Parent.Parent
repeat wait() until Player.Character
local Character=Player.Character
local Humanoid=Character:WaitForChild("Humanoid")
local Stats=Player:WaitForChild("leaderstats")
local Respawns=Stats:WaitForChild("Respawns")

function Search(Obj)
    for i,v in pairs(Obj:GetChildren()) do
        if v.ClassName=="Sound" then
            v:Stop()
        end
        Search(v)
    end
end

function Death()
    if Humanoid.Health<=0 then
        if Respawns.Value>0 then
            Respawns.Value=Respawns.Value-1
            Search(Player)
            wait(.2)
            Player.Backpack.Death:Play()
        end
    end
end
Humanoid.Changed:connect(Death)
Ad

Answer this question