So im making a 2x XP thing, like bloxfruits. How do i use os.time for a countdown, and be able to add seconds onto that os.time

So i have a datastore system, and theres gamepasses for 2x XP for hours, how would i save the os.time for the player, and add onto the time, incase you buy more hours

2 Likes

Save os.time() + timeAmountPurchased.
If you’re only counting when a player is in-game, then record when they leave so when they join you can recalculate.

How do i even use os.time in this case? if thats the right thing to use, maybe os.clock() I dont know

You save the time of purchase (os.time()) and every second you check if the current os.time() - timeOfPurchase >= lastingTime

What I would do is in your datastore save the time at which the multiplier will end (1h later than the current UNIX time).

That way you can easily check if it’s still active. To add extra time just add seconds to that timestamp (or current time if that’s higher).

1 Like

Since real-world time passage is of concern to you, it is necessary to use os.time or DateTime.now().UnixTimestamp. Either of these methods provides you with a Unix timestamp, which represents the number of seconds that have elapsed since the Unix epoch (January 1st, 1970). This timestamp is recorded in UTC (universal time). This is why it is ideal to us for understanding objective real-world time passage in programs.

A 2x EXP multiplier is better represented through a boolean that is stored in a short-term player data session (in a session that does not save). This can either be through a table or an attribute/BoolValue stored within the player. So long as this boolean is true, any EXP gained is doubled. Game-passes are lifetime purchases, so I believe you mean to implement your feature through a developer product. Your developer product will enable this multiplier for a set amount of time.

To best handle this, we’ll represent the persistence of the multiplier as an expiry date. Unlike the 2x EXP mutltiplier boolean, the expiry date will be stored in the long-term player data session. Default that value to nil.

We’ll set up a function to expire the 2x EXP multiplier:

local function expireDoubleExp(player: Player)
    local sessions = playerSessions[player]

    local longSession  = sessions.long
    local shortSession = sessions.short

    for secondsLeft = longSession.doubleExpExpiry, 1, -1 do
        longSession.doubleExpExpiry = secondsLeft
            
        task.wait(1)
    end

    longSession.doubleExpExpiry = nil
    shortSession.doubleExp = false
end

When the player enters the game, check if a 2x EXP multiplier expiry date exists. If so, invoke expireDoubleExp. Next, when the developer product is purchased:

local sessions = playerSessions[player]

local longSession  = sessions.long
local shortSession = sessions.short

-- Check again.
if not longSession.doubleExpExpiry then
    -- Initialize an expiry date.
    longSession.doubleExpExpiry = DateTime.now().UnixTimestamp + DOUBLE_EXP_SECONDS
    shortSession.doubleExp = true

    -- Expire the multiplier.
    expireDoubleExp(player)
else
    -- Top up the expiry date.
    longSession.doubleExpExpiry += DOUBLE_EXP_SECONDS
end
1 Like