Im trying to make a daily reward system, where it should save the time. Data does not save, prints out the line that says “data saved”. Maybe some of you guys have a clue what is going on? Thanks in advance!
local DataStoreService = game:GetService("DataStoreService")
local RewardDataStore = DataStoreService:GetDataStore("DailyRewards")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RewardEligibilityEvent = game.ReplicatedStorage:WaitForChild("RewardEligibilityEvent")
local REWARD_INTERVAL = 24 * 60 * 60 -- 24 hours in seconds
local MAX_REWARD_DAYS = 5 -- 5 days of rewards
local rewards = {
[1] = 100,
[2] = 200,
[3] = 300,
[4] = 400,
[5] = 500
}
local function getPlayerRewardData(player)
local success, data = pcall(function()
return RewardDataStore:GetAsync(player.UserId)
end)
if success and data then
print("we found data")
return data
else
-- Return default data if not found or an error occurred
return {
currentDay = 1,
lastClaimTime = os.time() -- Default to current time
}
end
end
local function savePlayerRewardData(player, data)
local success, errorMessage = pcall(function()
RewardDataStore:SetAsync(player.UserId, data)
print("saved data")
end)
if not success then
print("skibidi toilet")
warn("Failed to save data for " .. player.Name .. ": " .. errorMessage)
end
end
Players.PlayerAdded:Connect(function(player)
local rewardData = getPlayerRewardData(player)
local timeSinceLastClaim = os.time() - rewardData.lastClaimTime
if timeSinceLastClaim >= REWARD_INTERVAL then
rewardData.currentDay = math.min(rewardData.currentDay + 1, MAX_REWARD_DAYS)
RewardEligibilityEvent:FireClient(player, true, rewardData.currentDay)
else
RewardEligibilityEvent:FireClient(player, false, rewardData.currentDay)
end
player:SetAttribute("CurrentRewardDay", rewardData.currentDay)
player:SetAttribute("LastClaimTime", rewardData.lastClaimTime)
end)
Players.PlayerRemoving:Connect(function(player)
local rewardData = getPlayerRewardData(player)
rewardData.lastClaimTime = os.time()
rewardData.currentDay = rewardData.currentDay % MAX_REWARD_DAYS + 1
savePlayerRewardData(player, rewardData)
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.