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

Die upon losing all your lives script?

Asked by 9 years ago

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

Log in to vote
1
Answered by 9 years ago

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)

Ad

Answer this question