Hello there! So I am trying to make my daily reward timer working but it ended up broking.
The problem is when the timer hits 00:00:00, it became negative number and when the numbers is negative, player can claim everytime they want
Tried to find in dev forum but couldnt found any
Btw I have 2 scripts
Server Script:
local DataStoreService = game:GetService("DataStoreService")
local DailyRewardDS = DataStoreService:GetDataStore("NewDailyReward")
local dailyRewardRE = game.ReplicatedStorage.DailyRewardRE
local rewardsForStreak =
{
[1] = {5, 0},
[2] = {10, 0},
[3] = {15, 0},
[4] = {25, 50},
[5] = {40, 100},
}
game.Players.PlayerAdded:Connect(function(player)
local success, dailyRewardInfo = pcall(function()
return DailyRewardDS:GetAsync(player.UserId .. "-DailyRewards")
end)
if type(dailyRewardInfo) ~= "table" then dailyRewardInfo = {nil, nil, nil} end
local Money = player:WaitForChild("Money")
local Exp = player:WaitForChild("Exp")
Money.Value = dailyRewardInfo[1] or 0
local lastOnline = dailyRewardInfo[2] or os.time()
local CurrentTime = os.time()
local timeDifference
if lastOnline then
timeDifference = CurrentTime - lastOnline
end
local streak = dailyRewardInfo[3] or 1
local reward = rewardsForStreak[streak]
local ScreenGui = player.PlayerGui:WaitForChild("ScreenGui")
local DailyRewardsGUI = ScreenGui:WaitForChild("DailyRewards")
local ClaimButton = DailyRewardsGUI:WaitForChild("ClaimButton")
local Days = DailyRewardsGUI:WaitForChild("Days")
task.spawn(function()
while true do
task.wait(1)
local timeReset = (24*60*60) - (os.difftime(os.time(), lastOnline))
local hours = math.floor(timeReset / 60 / 60)
local mins = string.format("%0.2i", math.floor(timeReset / 60) - (hours * 60))
local secs = string.format("%0.2i", timeReset - (hours * 60 * 60) - (mins * 60))
local DrValue = game.ReplicatedStorage:WaitForChild("Value")
DrValue.Value = hours ..":".. mins ..":" .. secs
end
end)
if not timeDifference or timeDifference >= 24*60*60 then
local streak = dailyRewardInfo[3] or 1
local reward = rewardsForStreak[streak]
local rewardInfo = rewardsForStreak[streak]
local coins = rewardInfo[1]
local exp = rewardInfo[2]
DailyRewardsGUI.Visible = true
Days.Text = "Day " .. streak
if streak > 5 then streak = 1 end
ClaimButton.MouseButton1Click:Connect(function()
ClaimButton.Sound:Play()
Money.Value += coins
Exp.Value += exp
DailyRewardsGUI.Visible = false
local streak = streak + 1
if streak > 5 then streak = 1 end
local success, errormsg = pcall(function()
DailyRewardDS:SetAsync(player.UserId .. "-DailyRewards", {Money.Value, os.time(), streak})
end)
end)
elseif timeDifference then
task.wait((24*60*60) - timeDifference)
if game.Players:FindFirstChild(player) then
local streak = dailyRewardInfo[3] or 1
local reward = rewardsForStreak[streak]
local rewardInfo = rewardsForStreak[streak]
local coins = rewardInfo[1]
local exp = rewardInfo[2]
local DailyRewardsGUI = player.PlayerGui:WaitForChild("DailyRewards")
local ClaimButton = DailyRewardsGUI:WaitForChild("ClaimButton")
local Days = DailyRewardsGUI:WaitForChild("Days")
DailyRewardsGUI.Visible = true
Days.Text = "Days " .. streak
ClaimButton.MouseButton1Click:Connect(function()
ClaimButton.Sound:Play()
Money.Value += coins
Exp.Value += exp
DailyRewardsGUI.Visible = false
local streak = streak + 1
if streak > 5 then streak = 1 end
local success, errormsg = pcall(function()
DailyRewardDS:SetAsync(player.UserId .. "-DailyRewards", {Money.Value, os.time(), streak})
end)
end)
end
end
end)
Local Script :
game.ReplicatedStorage.Value.Changed:Connect(function()
script.Parent.Text = "Daily Reward: <font color=\"rgb(255,85,0)\">" .. game.ReplicatedStorage.Value.Value .. "</font>"
end)