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)