How to make a expiring timer

I want to make a timer that when u use something u cannot use it for like 2 hours and it has a timer with hours, minutes, seconds when the player leaves the game the timer goes on and when he comes back in 2 hrs he can u to something again

1 Like

to do that you need to have datastore
first save use time in datastore and to get the time :

local dt = DateTime.now()

and when he use you it again check if use time - DateTime.now() < the time you want

to more information :
savedata
(DateTime)
and to format time

i hope that help you :slight_smile:

Can it be tick() instead? If not how do u use datetime what does it return

you can use os.time() better than tick() os
but datetime can be used to easily format dates and times in specific locales

How would I get hours minutes and seconds from tick()?

local timeTook = tick() - startTime

local minutes = math.floor(timeTook / 60)
timeTook = timeTook - (minutes * 60)

local seconds = math.floor(timeTook)
timeTook = timeTook - seconds

local milliseconds = math.floor(timeTook * 1000)
print(string.format("%d:%d.%d", minutes, seconds, milliseconds))

What is the start time variable?

the tick() when first use is start time variable

Wdym? There is no variable in there called starttime

i mean to get the time when u use something

You sound like you have no knowledge in scripting. I’d advise you learn the basics of scripting instead of relying on someone to make a whole script for you

I do? I just don’t have much knowledge in making no timers etc

But u have no variable such as local starttime

wdym do you mean that start time is time to first use or what?

Oh is it something to do with datetime?

if you mean time to first use so yea

local startTimes = {}

function OnPlayerAdded(player)
    startTimes[player.UserId] = os.time()
end

function GetCurrentTime(player)
	local timeDifference = os.difftime(os.time(), startTimes[player.Name])
	local hours = math.floor(timeDifference / 3600)
	local minutes = math.floor((timeDifference - (hours * 3600)) / 60)
	local seconds = timeDifference - ((hours * 3600) + (minutes * 60))

	return hours, minutes, seconds
end

function ResetTimer(player)
    startTimes[player.UserId] = os.time()
end

game.Players.PlayerAdded:Connect(OnPlayerAdded)

This will start a timer when a player joins, then you call GetCurrentTime and it will tell you how long the timer has been running for that player. You can also call ResetTimer to restart the timer to 0 for that player.

It works by storing time stamps and getting the last value. If you want the player to be able to store timer data across servers, you need to just add the startTimes to a data store.

Hope that helps.