Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I fix this Death script?

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

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.