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

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

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. :-)

1 comment

0

Lua, not LUA.

2 answers

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.

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.

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