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?
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