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

How to add a timer so the player holding the object will die?

Asked by 6 years ago

Hey guys, So, I'm fairly new to LUA scripting and would love some help.

So basically, I want to add a timer, and when the timer runs out the person with the object last, dies. Kind of like time bomb.

As I said, I'm really new to this, so I'd love a helping hand. :-)

0
Lua, not LUA. User#24403 69 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

There are events for Tool.Equipped:Connect()

You can start a timer in the script then access the player's humanoid and set the health below or equal to zero. Remember to also use Tool.Unequipped:Connect() or disconnect the event.

Ad
Log in to vote
0
Answered by 6 years ago

Put this inside the tool instance itself (not the part, the tool) as a normal script (not a local or module)

local tool = script.Parent
local currentime = 0 -- Current Time (once it equals timend, it will make the player with it die)
local timend = 5 -- Time max (in seconds)

function toolEquipped()
    begin()
end

tool.Equipped:connect(toolEquipped)
local db = 0
function begin()
if db == 0 then
db = 1

function getHumanoid()
    local affirmative = false -- Will be used to determine if it gets a humanoid (player)
    local hmd
    if tool.Parent:IsA("Model") then
        hmd = tool.Parent:FindFirstChild("Humanoid")
        if hmd then
            affirmative = true
        end
    end
    if affirmative == true then
        return hmd
    else
        return nil
    end
end
local humanoid
repeat
    humanoid = getHumanoid()
    if humanoid == nil then

    else
        currentime = currentime + 0.1
    end
    wait(0.1)
until currentime >= timend
humanoid.Health = 0
end
end

This script however will not make anything die if it's not equipped by a player.

But I did make it so it will only do the counting only IF equipped by a humanoid/player.

Answer this question