How do you make a real life timer? Like one in Clash Of Clans?

  1. What do you want to achieve? Keep it simple and clear!

How to make a timer that continues even if the player leaves. Like it gets the current time + The upgrade time but I can’t get it to work.

  1. What is the issue? Include screenshots / videos if possible!
    I can’t make a real life timer

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes, but most real world time things are unrelated and don’t help

I recommend using os.date. It tracks irl time on UTC.
Here is the os.date documentation stuff incase you want it:

So just do the current date + the time? I’m not sure as it didn’t help much.

My preferred way of tracking stuff like this is to use tick(), which returns how many seconds it has been since the unix epoch. If you want to make a real-time upgrade timer, you can use tick() + how many seconds the upgrade will take to complete.

Would this work?

local part = script.Parent

local upgrade_time = 60 * 1

local time = tick() + upgrade_time

part.Touched:Connect(function()
	if tick() == time then
		print("hey!")
	else
		warn("Hey!")
	end
end)

You’ll want to use tick() when you start the timer.

use “tick()” to measure real time

I don’t really understand much.

local part = script.Parent

local upgrade_time = 60 * 1



part.Touched:Connect(function()
	local Time = tick() + upgrade_time
	if tick() == Time then
		print("hey!")
	else
		warn("Hey!")
	end
end)

If you want the upgrade to start when you touch the part, do this;

local part = script.Parent

local upgrade_time = 60 * 1
local finishTime;

part.Touched:Connect(function()
	if not finishTime then --If there isn't a set finish time
		finishTime = tick() + upgradeTime; --Start upgrade timer
	elseif tick() >= finishTime then
		print("Upgrade finished!");
	elseif tick() < finishTime then
		warn("Upgrade still in progress!");
	end
end)

tick() returns how many seconds have passed since January 1st 1970. When you run it, you’ll get a bigger number each time, because time has gone forward since the last time you ran it.

1 Like

Whenever the event fires I get this error:

Workspace.Part.Script:11: attempt to compare function < number

I accidentally put tick instead of tick() on that last elseif part.

1 Like

Last thing. Does it continue even if the player leaves?

You’ll need to save the finishTime variable somewhere in a datastore if you want it to transfer between sessions.

1 Like