I’m creating a daily rewards system and everything is going well. The player receives the reward and the streak increases. The problem I’m having is making sure that the Gui is still visible after every time the player joins the game but not allowing the player to claim their reward if it has not been 24 hours.
It’s giving the reward to the player every time the click the button. I’ve added an if statement and it still doesn’t work. Any idea what I’m doing wrong?
local RewardsTable = {
[1] = 1;
[2] = 2;
[3] = 3;
[4] = 4;
[5] = 5;
}
local CanBeClaimed = false
game.Players.PlayerAdded:Connect(function(Player)
local PlayerGui = Player.PlayerGui
local DailyRewards = PlayerGui:WaitForChild("IntroGui").DailyRewards
local ClaimButton = DailyRewards.ClaimButton
local TimerLabel = DailyRewards.Detail.TimerLabel
local Success, DailyRewardsInfo = pcall(function()
return DataStore:GetAsync(Player.UserId.."-DailyRewards")
end)
if type(DailyRewardsInfo) ~= "table" then DailyRewardsInfo = {nil, nil, nil} end
local DreamCoins = Player.PlayerValues.DreamCoins
local LastOnline = DailyRewardsInfo[2]
local CurrentTime = os.time()
local TimeDifference
if LastOnline then
TimeDifference = CurrentTime - LastOnline
end
if not TimeDifference or TimeDifference >= 24*60*60 then
local Streak = DailyRewardsInfo[3] or 1
local Reward = RewardsTable[Streak]
CanBeClaimed = true
TimerLabel.Text = "CLAIM YOUR REWARD!"
ClaimButton.MouseButton1Click:Connect(function()
if CanBeClaimed == true then
DreamCoins.Value = DreamCoins.Value + Reward
end
local Streak = Streak + 1
CanBeClaimed = false
TimerLabel.Text = "YOU'VE ALREADY CLAIMED YOUR REWARD"
if Streak > 5 then Streak = 1 end
local Success, ErrorMessage = pcall(function()
DataStore:SetAsync(Player.UserId.."-DailyRewards", {os.time(), Streak})
end)
end)
elseif TimeDifference then
wait((24*60*60) - TimeDifference)
CanBeClaimed = true
if game.Players:FindFirstChild(Player) then
local Streak = DailyRewardsInfo[3] or 1
local Reward = RewardsTable[Streak]
TimerLabel.Text = "CLAIM YOUR REWARD!"
ClaimButton.MouseButton1Click:Connect(function()
if CanBeClaimed == true then
DreamCoins.Value = DreamCoins.Value + Reward
end
local Streak = Streak + 1
CanBeClaimed = false
TimerLabel.Text = "YOU'VE ALREADY CLAIMED YOUR REWARD"
DailyRewards.Visible = false
if Streak > 5 then Streak = 1 end
pcall(function()
DataStore:SetAsync(Player.UserId.."-DailyRewards", {os.time(), Streak})
end)
end)
end
end
end)
Thank you if you’re willing to read this and help me.