Death timer not working local script

I am making a death timer and this is a segment of my script, I’ve tried remote events and all and idk what to do (this is a local script btw) I am trying to kill the player.
Your time would be much appreciated!

if currentTime == 0 then
	local killEv = game:GetService("ReplicatedStorage").KillEvent
	char.Humanoid.Health = 0
end
1 Like

Is that the full script? By the way, try doing this:

if currentTime <= 0 then
	
end

instead of:


if currentTime == 0 then
	
end

What’s the problem here? Any errors?

This was only a segment of it but i highlighted the main problem.

Nope

local label = script.Parent
local currentTime = 10
local player = game:GetService("Players").LocalPlayer
local char = player.Character

local function formatTime()
	local hours = math.floor(currentTime / 3600)
	local minutes = math.floor((currentTime - (hours * 3600))/60)
	local seconds = (currentTime - (hours * 3600) - (minutes * 60))
	local format = "%02d:%02d"
	return format:format(minutes, seconds)
end

local function startTimer()
	while true do
		currentTime -= 1
		label.Text = formatTime()
		task.wait(1)
	end
end

startTimer()

if currentTime <= 0 then
	local killEv = game:GetService("ReplicatedStorage").KillEvent
	char.Humanoid.Health = 0
end

Why are you putting that segment right after startTimer()?
It’ll run immediately after hence disabling the currentTime <= 0 check when it really hits 0

(ACTUALLY IGNORE THIS, JUST REALIZED YOUR STARTTIMER() IS NOT IN TASK.SPAWN())

I changed it its been a few months since ive scripted

You can do this instead:

local function startTimer()
	while true do
		currentTime -= 1
		label.Text = formatTime()

		if currentTime <= 0 then
			-- do your thing here (kill the player, reset the timer, etc.)

			break -- don't forget to break out of the loop
		end

		task.wait(1)
	end
end

startTimer()
1 Like

If you use while true do after the end , it will not run. The while true loop creates an infinite loop, and the code after it will never be executed if i am right try using RunService. i might also be false…

Thanks guys, guess I didnt break the loop and added the right format.

2 Likes

Just to add, your original code does actually work. You just forgot to break out of the loop after the currentTime hits 0 making the while loop run infinitely → never reaching the “if currentTime <= 0” line

3 Likes

yeah I was gonna add a break once i got the humanoid kill part done.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.