Daily reward system not saving time

I have a datastore that saves players points and am trying to set up a daily reward system for when the player joins and claims the reward, they don’t get another until 24 hours have passed. The problem is that the claim daily reward gui keeps showing up every time I join. Now everything else works fine so I’m guessing it has something to do with the os.time() not saving properly when the player first claims the reward.

local DataStore = game:GetService("DataStoreService"):GetDataStore("PointDataStore")
local moduleScript = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))

local currencyName = "Points"

local hourWait = 24

game.Players.PlayerAdded:Connect(function(player)
	local pointFolder = Instance.new("Folder", player)
	pointFolder.Name = "leaderstats"	

	local pointAmount = Instance.new("IntValue", pointFolder)
	pointAmount.Name = currencyName

	local ID = currencyName.."-"..player.UserId
	local savedData

	local timeNow = os.time()

	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	if savedData ~= nil then
		--returning player to the game
		local timeSinceLastClaim = timeNow - savedData

		if (timeSinceLastClaim / 3600) >= hourWait then
			--eligible for reward
			print("daily reward is eligible") --keeps printing every time I join
			local dailyRewardShow = game.ReplicatedStorage.dailyRewardShow
			dailyRewardShow:FireClient(player, hourWait)
			local connection
			connection = game.ReplicatedStorage.dailyRewardClaimRegular.OnServerEvent:Connect(function(triggeringPlayer)
				if triggeringPlayer == player then
					player.leaderstats[currencyName].Value += 4
					DataStore:SetAsync(player.UserId.."-dailyReward", os.time())
					connection = nil
				end
			end)
		end

		pointAmount.Value = savedData
		print("Data loaded")
	else
		--new player
		pointAmount.Value = 10
		print("New player to the game")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	local data
	pcall(function()
		data = DataStore:GetAsync(player.UserId.."-dailyReward")
	end)
	if data == nil then
		pcall(function()
			local timeVal = os.time()
			DataStore:GetAsync(player.UserId.."-dailyReward")
		end)
	end
	DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
end)
2 Likes

The main problem is located here. You’re subtracting the current time by the saved points, instead of the saved last time. I just assume you forgot to retrieve the last time point, or got mixed up with variables.

1 Like

Yep that was the issue lol. Added an empty variable and put it to equal another dataStore:GetAsync with the -dailyReward key within the first pcall. Thanks!!

2 Likes