Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What method would I use to track time?

Asked by
Spooce 78
10 years ago

What method would I need to use to track the amount of time a player has to wait until his/her next bonus. Say they have to wait 24 hours until their next bonus, how would I keep track of that?

0
When the bonus is given, put the current time (http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#os.time) in a Data Store and then make a loop that compares the last bonus time, with the current time. If it's more then 24 hours, give them a bonus. damagex443 325 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago

I would first save the value os.time() returns into a Data Store.

Then I'd use os.difftime() (when a player joins) to find the difference between os.time() and the Data Store key.

Example:

local ds = game:GetService("DataStoreService") --Gets the data store service
local bonus = ds:GetDataStore("BonusTime") --Generates a Data Store called BonusTime if it doesn't exist. Otherwise, it just gets the BonusTime data store.

game.Players.PlayerAdded:connect(function(player) --When the player is added.
    local bonusKey = "user_" .. player.userId --The key is unique to each user.
    if bonus:GetAsync(bonusKey) ~= nil then --If there is a value in the key then
        print("Last bonus time found") --Testing purposes, you can remove this.
        local time2 = bonus:GetAsync(bonusKey) --Makes a variable with the value of the key.
        if os.difftime(os.time(),time2) >= 86400 then --If the difference between the current time and the bonus time of the player is over or equal to 24 hours (in seconds) then.
            print("Giving bonus to player") --Testing purposes, you can remove this.
            --Give player bonus points and notify them
            print("Saving bonus time") --Testing purposes, you can remove this.
            bonus:UpdateAsync(bonusKey, function(oldVal) --Updating the old value to the current time.
                newVal = oldVal or 0 --In case the oldVal is nil
                newVal = os.time() --Sets newVal to the current time.
                return newVal --Saves the newVal to the key.
            end)
        end
    else --If there is no value in the key then give the player a bonus for the first time and save the current time.
        print("Giving bonus to player") --Testing purposes, you can remove this.
        --Give player bonus points and notify them
        print("Saving bonus time") --Testing purposes, you can remove this.
        bonus:UpdateAsync(bonusKey, function(oldVal)
            newVal = oldVal or 0
            newVal = os.time()
            return newVal
        end)
    end
end)

NOTE: I have not tested this, read the Data store wiki page on the ROBLOX Wiki to find out how to test Data stores correctly. You WILL need to add the part where bonus points are added yourself.

Data store wiki page: http://wiki.roblox.com/index.php?title=Data_store#Testing_Data_Stores

Ad

Answer this question