How Would I Go About Making a boost last for a certain amount of time?

Hello everyone, I am making a devproduct, that when bought, will give a player a boost for a certain amount of hours. I haven’t really done a dev product like this, so I don’t really know where I should start.

I think the best way to make a boost last a certain amount of time is by using os.time() to check if the boostLength - (currentTime - purchaseTime) <= 0. Heres an example:

local boostLength = 3600 -- the time in seconds of how long you want the boost to last
local boostStartTime = os.time() -- the time that they buy the boost

-- how to check if the boost has ended:
local timeRemaining = boostLength - (os.time() - boostStartTime) -- calculates the remaining time
if timeRemaining <=0 then -- you could also check if os.time() - boostStartTime >= boostLength if you don't want to display how much time they have left.
print("Boost has ended")
print(string.format("Boost still active, %s time remaining", tostring(timeRemaining))

If you want you can turn the time left in seconds into days, hours, minutes and seconds.

Hope this helps!

Thank you. So the only value I would have to save* is the boost start time? Or is there a way to get the time that the player bought a dev product?

*Data store save

Yeah you need to save when they bought it either in a table or in a datastore when they leave if you want them to keep the boost when they rejoin.

Also, I would have to put the following code in a while true do loop, right?

yeah if you want to continuously check if their boost has ran out then you should do that.

I would recommend checking once every second, as it won’t be very off and will make a smooth counter if you want to show the player how much time they have left.

1 Like

Question, what does task.wait(3) do, in this situation?

I just put it there so if you ran the code it would start the boost timer and then wait three seconds before printing how much time was left. It’s irrelevant.

1 Like