I have this script which activates on Death:
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() Player.Backpack.Death:Play() end end end Humanoid.Changed:connect(Death)
It sometimes fires multiple times when you die however and takes too many lives away from your stats. How can I fix this?
Why not use the .Died
event? It fires when a Player's Humanoid reaches 0 [or, in other words, when the Humanoid dies];
local function Death() --Heres the function you used before, I only added a 'local' [it's a habit now] if Player and Character and Humanoid and Respawns and Respawns.Value > 0 then --This checks to see if 'Player', 'Character', 'Humanoid', and 'Respawns' exist, also will check if 'Respawns''s Number Value is over 0 Respawns.Value=Respawns.Value-1 --This will take away one from the Value [like 2 - 1 = 1, 5 - 3 = 2, ect.] Search(Player) --Heres the function your using if Player:FindFirstChild("Backpack") and Player.Backpack:FindFirstChild("Death") then --You forgot to check if 'Death' exists, might've caused an error :P Player.Backpack.Death:Play() --This will Play 'Death' end --This ends the code block for the 'if' statement end --This ends the code block for the 'if' statement end --This ends the code block for the function Humanoid.Died:connect(Death) --This connects 'Death' too the Humanoid; When the Humanoid dies, it will fire the event
Hope this helped!