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

Die upon losing all your lives script?

I'm working on a brick that makes you lose a live everytime you touch it. Here's the script:

function onTouched(hit)
local hitname = tostring(hit.Parent)
	if game.Players:findFirstChild(hitname) then
	local player = game.Players:findFirstChild(hitname)
	local lives = player:findFirstChild("Lives")
	lives.Value = lives.Value - 1
	end	
end

script.Parent.Touched:connect(onTouched)

Is there a way to make the script kill you upon touching the brick if you have 0 lives?

1 answer

✓ Accepted answer

First of all add debounce so that the player who touches the brick doesn't lose all their lives when only touching once.

local db = false --Our debounce
function onTouched(hit)
if db == false then
    db = true
    local hitname = tostring(hit.Parent)
    if game.Players:findFirstChild(hitname) then
           local player = game.Players:findFirstChild(hitname)
           local lives = player:findFirstChild("Lives")
           lives.Value = lives.Value - 1
       end
    wait(1) -- Wait 1 second before losing a life again when touched.
    db = false 
    end 
end

script.Parent.Touched:connect(onTouched)

Next we want to kill the player when the player has lost all their lives so we add this:

local db = false --Our debounce
function onTouched(hit)
if db == false then
    db = true
    local hitname = tostring(hit.Parent)
    if game.Players:findFirstChild(hitname) then
           local player = game.Players:findFirstChild(hitname)
           local lives = player:findFirstChild("Lives")
           lives.Value = lives.Value - 1
           if lives.Value <= 0 then --If the player has lost all their lives then..
               hit.Parent.Humanoid.Health = 0 --Kills the player
           end
       end
    wait(1) -- Wait 1 second before losing a life again when touched.
    db = false 
    end 
end

script.Parent.Touched:connect(onTouched)

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