so i made a daily reward system that gives cash each day but the thing is when my day 1 cash is a sunday and i claimed it and come back at tuesday,i should be getting day 2 cash but seems like it skips and gives me day 3 cash and i dont know how to fix it
The system works by sending a remote event with day amount as a param to player ui if the player has the cash then shows the ui then i would send a remote back to server with days param when player click claim then it gives me money in server
local config = require(game.ReplicatedStorage:WaitForChild("RewardSystem"):WaitForChild("RewardConfig"))
local DataStoreService = game:GetService("DataStoreService"):GetDataStore("abc") -- Define the Datastore service and define our datastore name
local id = 17373749
local mps = game:GetService("MarketplaceService")
local days = {}
game.Players.PlayerAdded:Connect(function(Player)
local LastLogin -- Define a variable that we will use later.
pcall(function()
LastLogin = DataStoreService:GetAsync(Player.UserId) -- Check if the player joined already in the past 24 hours
end)
if LastLogin and (os.time() - LastLogin.Unix >= 86400) then
-- os.time() returns how many seconds have passed since the Unix epoch (1 January 1970, 00:00:00)
-- 86400 is the number of seconds in one day
days[Player.UserId] = LastLogin.Days + 1
if days[Player.UserId] > 7 then
days[Player.UserId] = 1
end
if mps:UserOwnsGamePassAsync(Player.UserId,id) then
game.ReplicatedStorage.RewardSystem.ShowGui:FireClient(Player, days[Player.UserId],true)
else
game.ReplicatedStorage.RewardSystem.ShowGui:FireClient(Player, days[Player.UserId],false)
end
elseif not LastLogin then -- If the player never joined the game before
days[Player.UserId] = 1
if mps:UserOwnsGamePassAsync(Player.UserId,id) then
game.ReplicatedStorage.RewardSystem.ShowGui:FireClient(Player, days[Player.UserId],true)
else
game.ReplicatedStorage.RewardSystem.ShowGui:FireClient(Player, days[Player.UserId],false)
end-- We fire an event ()
else
end
end)
game.ReplicatedStorage.RewardSystem.GiveMoney.OnServerEvent:Connect(function(plr,day,owns)
if plr and day then
local cash
if owns then
for i,v in pairs(config.viprewards) do
if i == tostring(day) then
cash = v
break
end
end
else
for i,v in pairs(config.rewards) do
if i == tostring(day) then
cash = v
break
end
end
end
if cash ~= nil then
plr.leaderstats.Cash.Value += cash
game.ReplicatedStorage.Miscs.Ui.XpAndCashNotify:FireClient(plr,"daily reward",cash)
else
warn("error in DailyReward line 51")
end
DataStoreService:SetAsync(plr.UserId, {
Unix = os.time(),
Days = days[plr.UserId]
}) -- We update the player datastoreto the current Unix epoch
end
end)
Thanks for any help