Hi, I was wondering how to make special offers that disappear for the player after a while in real life. For example, a starter pack you can buy. If you don’t buy it within 3 days, it will just disappear. How to make the action happen after 3 days in real life?
Log when a player has started playing your game for the first time, and use tick() or some other way to get the current time and log that as well. If the player joins back to the game, compare the time they joined for the first time to the current time.
If the passed time has exceeded x days, then don’t show the offer to the player. I know this sounds very confusing, so I made this flowchart to make things easier to understand
I like how you drew an algorithm flowchart @EmeraldLimes! It’s exactly how they should do it. Using a picture is a much better way to explain the workflow in this case.
Just to add to that, don’t use tick(). It has been recently deprecated. Even though it still works, Roblox discourages developers from using it. Instead, os.time() is a much better option. For benchmarking, use the very precise os.clock(), and for general time difference measurement, use time() - starts with server.
One aspect more to cover are Data Store limits (read more about them here: Data Stores | Roblox Creator Documentation). While on the subject, these limits are deliberate to protect servers, but at the same time prevent us from saving so much data to one specific key.
Therefore a better approach is saving starter pack information with other player’s data to their own keys.
@EmeraldLimes already gave you the answer. The following code is just intended to enable you an easier start.
Part of the above flowchart as a code here.
local DataStoreService = game:GetService("DataStoreService")
local SampleDS = DataStoreService:GetDataStore("SampleDataStore")
local STARTER_OFFER_EXPIRATION = (3600 *24) *3 --> 3 days in seconds
local joinTimes = {}
--[[
Suppose "data" variable stores already retrieved data.
When player leaves, save their joinTime[player.UserId]
even if it's nil. That will mean player is not a newcomer.
]]
local function playerWasAdded(player)
local success, data = nil, nil
if (success and not data) then
-- If there was no error and no data, assume player is new.
joinTimes[player.UserId] = os.time()
-- Offer starter pack here.
elseif (success and data) then
joinTimes[player.UserId] = data["JoinTime"] -- might be nil
if (
joinTimes[player.UserId] and
os.time() - joinTimes[player.UserId] < STARTER_OFFER_EXPIRATION
)
then -- Offer purachse here.
end
end
end
When player leaves, don’t forget to clear their key from joinTimes table (by setting it to nil).
Basics of saving players’ data: Saving Data | Roblox Creator Documentation.
Now I understand how should I do this. Thank you two!