Help with Free Gifts System DataStore

Hi, I need help with a free gift system I am doing.

My system is a GUI that has different buttons with timers (I will show it below) and at the end of that timer the player can claim the gift and get money. The problem is that I don’t know how to make a datastore to avoid that the player after claiming the gift can leave the game and when joining again he can claim again the gift. I was also looking for the DataStore to be renewed every 24 hours, I mean, that when 24 hours pass since the player claims the gift he can claim it again (something similar to a daily gift system) but I don’t know how to do it in a UI.

UI:

UI for the timers and claim

  • Local Script on the UI:
local mainFrame = script.Parent.Parent.Frame:WaitForChild("MainBG")
local giftsFrame = mainFrame:WaitForChild("GiftsFrame")
local openSoundSFX = script.Parent.Parent:WaitForChild("OpenFreeGiftSFX")

local claimEvent = game.ReplicatedStorage.ClientEvents:WaitForChild("MoneyEvents").FreeGiftClaim

local gift = giftsFrame:WaitForChild("Gift1")

local giftNotificationIcon = script.Parent.Parent:WaitForChild("TriggerOpen").GiftNotification

local minutes = 5 -- 5
local seconds = 0

local alreadyClaimed = gift:WaitForChild("Claimed")
alreadyClaimed.Value = false

-- Timers

--- Main function
	repeat
		if seconds <= 0 then
			minutes = minutes - 1
			seconds = 59
		else
			seconds = seconds - 1
		end
		if seconds < 10 then
			gift.Timer.Text = tostring(minutes)..":0"..tostring(seconds)
		else
			gift.Timer.Text = tostring(minutes)..":"..tostring(seconds)
		end
		wait(1)
	until minutes <= 0 and seconds <= 0 wait(0.2) giftNotificationIcon.Visible = true



gift.MouseButton1Click:Connect(function()
	if minutes <= 0 and seconds <= 0 then
		if alreadyClaimed.Value == false then
			alreadyClaimed.Value = true
			openSoundSFX:Play()
			local reward = 20 -- change
			claimEvent:FireServer(reward)
			gift.ClaimedText.Visible = true
			
			giftNotificationIcon.Visible = false
			
		end
	end
end)

Hope I can get some help. Greetings.

By using the following function which returns the amount of seconds left till the next day:

	local function getSecondsTillTomorrow()
		local tomorrow = os.date("!*t", os.time() + 86400)
		local tomorrowSeconds = os.time({
			year = tomorrow.year, 
			month = tomorrow.month, 
			day = tomorrow.day,
			hour = 0, 
			min = 0, 
			sec = 0
		})
		local secondsUntilTomorrow = tomorrowSeconds - os.time()
		return secondsUntilTomorrow
	end

you can use this to return the amount of seconds left to the player upon joining and continue counting down locally to not stress the server.

Store the date of the last claimed gift for each player in the datastore, which will be used to know whether there should be a free gift or not, by comparing this stored value to today’s value:

local todayConverted = today.day .. today.month .. today.year
if playerData["lastGiftDate"] ~= todayConverted then -- if different date
	playerData["lastGiftDate"] = todayConverted
	-- give gift to player
end

When the player hits 0 seconds locally, fire a remote to the server and use the same code above to check whether a new day has started and do the same.