Script in ServerScriptService
local DataStore = game:GetService("DataStoreService"):GetDataStore("DailyRewards")
local hourWait = 0.0000000000000001
local possibleRewards = {1000,2000,1000,1000,1000,500,500,500,500,500,500,500,500,500,500,1000,1000,1000,2000,2000,10000}
game.Players.PlayerAdded:Connect(function(player)
local cash = player.leaderstats.Cash.Value
local timeNow = os.time()
local data
pcall(function()
data = DataStore:GetAsync(player.UserId.."-dailyReward") -- 14930679-dailyReward
print("Getting Data")
end)
if data ~= nil then
-- Returning player to the game
local timeSinceLastClaim = timeNow - data -- Number in sec since the last reward was claimed
print("Time since last claim: "..timeSinceLastClaim)
if (timeSinceLastClaim / 3600) >= hourWait then
-- They are eligible for a reward
local reward = possibleRewards[math.random(1,#possibleRewards)]
wait(3)
game.ReplicatedStorage.ShowDailyReward:FireClient(player,hourWait,reward)
local connection
connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(triggeringPlayer)
if triggeringPlayer == player then
print("Reward Claimed")
cash = cash + reward
DataStore:SetAsync(player.UserId.."-dailyReward",os.time())
connection:Disconnect()
end
end)
else
print("Player is uneligible right now")
end
else
-- New Player
print("New Player")
wait(3)
local reward = possibleRewards[math.random(1,#possibleRewards)]
game.ReplicatedStorage.ShowDailyReward:FireClient(player,hourWait,reward)
local connection
connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(triggeringPlayer)
if triggeringPlayer == player then
print("Reward Claimed")
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + reward
DataStore:SetAsync(player.UserId.."-dailyReward",os.time())
connection:Disconnect()
end
end)
end
end)
LocalScript in StarterGui
game.ReplicatedStorage.ShowDailyReward.OnClientEvent:Connect(function(hoursToNextReward, rewardAmount)
wait(3)
script.Parent.Enabled = true
script.Parent.MainFrame.ComeBackText.Text = "Thank you for playing! Come back in "..hoursToNextReward.."for your next reward*."
script.Parent.MainFrame.Claim.MouseButton1Click:Connect(function()
game.ReplicatedStorage.ClaimReward:FireServer()
script.Parent.RewardNotice.Text = "You have received "..rewardAmount.."!"
script.Parent.RewardNotice:TweenPosition(UDim2.new(0.5, 0,0.1, 0), "InOut", "Quad", 0.5, true)
wait(2)
script.Parent.MainFrame.Visible = false
wait(1.5)
script.Parent.RewardNotice:TweenPosition(UDim2.new(0.5, 0,-0.1, 0), "InOut", "Quad", 0.5, true)
wait(1.5)
script.Parent.Enabled = false
script.Parent.MainFrame.Visible = true
end)
end)