So, for my game, we have a special “Daily Group Reward” area where people in the game’s group will be able to get gems every 24 hours.
But me, being the inexperienced scripter I am, have no clue how to make a datastore for this, as I don’t know how you would keep the time when a server shuts down or restarts, because if a player joins a different server, the time may be different or something. I essentially want it to be, they touch a part, and if it has been 24 hours since the last touch, they get gems, and if not, I want a specific text label to pop up saying they can’t collect yet, or somehow I would like to do a little timer for their next collect, but I don’t know how you would do that for every user separately.
If anybody could possibly provide a link or some base code to build off of, it would be greatly appreciated. (And no, I don’t want to be spoon fed the answer, I like to actually have to do the main body.)
One method you could use is concatenating the name of the datastore with the day, month and year. There is no limit on how many datastores your game can have, so you could utilize this. Although this is pretty easy, I don’t think it would be the best option for this use case.
Another solution, which I think might be the best one, is datastoring os.time when they claim the reward. With this method, whenever they touch a part, you could see if it’s been 24 hours and if it has, you can reward them accordingly and update the datastore. Here is a quick sample that should set you on the right track.
local currentTime = os.time()
local timeDifference = currentTime - lastOnlineTime
if not lastOnlineTime or timeDifference >= 24*60*60 then
--looks like the player is eligable for a reward!
else
--looks like the player isn't eligable for the reward!
end
In this example, lastOnlineTime would be the value of os.time which is being datastored. If you need help understanding anything or would like further explanation, let me know.
I’ve literally never used a datastore before, my other friend that scripts will usually do that, but I’m attempting this on my own for the first time.
I’ll explain a little more specifically what I’m trying to do;
So I have a stand, and I have a part that I would like to set up so that when you collide with it, it will check if you have claimed gems within the last 24 hours. If not, it will make a GUI .Visible (That part isn’t hard.) Then it will set a timer, essentially, for 24 hours. If you HAVE claimed within the last 24 hours, I want it to say how long until the next claim on a GUI (Again, GUI’s aren’t the hard part, it’s doing a timer for it.)
Just looked at that, but I am going to attempt to do it with a datastore, first. I may try this after I figure out the datastore or when @alphadoggy111 responds.
I don’t have a link to sample code for this, but this is what you want to do:
Record the last time the player claimed a daily reward using tick(), os.time for UTC, or anything else that will avoid timezone issues
When the player touches the brick, if the current time minus the time at which the player last claimed the daily reward is greater than or equal to 24 hours, give them the daily reward, and store the new time in the database. Otherwise, don’t give them the reward and leave the last claimed time the same.
—> In either case, you can also return the last claimed time, so that the player knows how much longer they have to wait for the next daily reward
You can accomplish this using just one data store with just the logic above. I know you are just learning how to do this, but please don’t use multiple data stores for this, especially as a beginner project.
Sorry, went AFK for a second there. Here’s an example of something you could do. Please don’t actually use this as this doesn’t have a debounce or anything which you will 100% need working with Touched events and datastores, as well as it not being well optimized. This is simply trying to illustrate the usage of datastores and stuff.
local DSS = game:GetService("DataStoreService")
local ClaimData = DSS:GetDataStore("RewardDS")
local TouchPart = workspace:WaitForChild("PartToTouch")
local function CheckRewardEligability(hit)
local currentTime = os.time()
if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
local player = game.Players:FindFirstChild(hit.Parent.Name)
local success, lastLogin = pcall(function()
return ClaimData:GetAsync(player.UserId)
end)
if lastLogin == nil then
lastLogin = 0
end
local timeDifference = currentTime - lastLogin
if timeDifference >= 24*60*60 then
-- give reward
ClaimData:SetAsync(player.UserId, currentTime)
else
-- no reward, make ui visible
local minutesLeft = (((lastLogin + 86400) - currentTime) / 60)
player.PlayerGui.ScreenGui.Label.Text = "Reward claimable in ".. math.floor(minutesLeft).. " minutes"
player.PlayerGui.ScreenGui.Enabled = true
end
end
end
workspace.PartToTouch.Touched:Connect(CheckRewardEligability)