Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- All scripts are in ServerScriptService
- -- Daily Reward Script; "Script"
- local DataStore = game:GetService("DataStoreService"):GetDataStore("DailyRewards")
- local hourWait = 0.0006
- local possibleRewards = {10,15,10,10,5,5,5,5,5,10,10,15,50}
- local defaultCash = 0
- game.Players.PlayerAdded:Connect(function(player)
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = player
- local coins = Instance.new("IntValue")
- coins.Name = "Coins"
- coins.Parent = leaderstats
- local timeNow = os.time()
- local playerData = Instance.new("Folder")
- playerData.Name = player.Name
- playerData.Parent = game.ServerStorage.PlayerData
- local data
- pcall(function()
- data = DataStore:GetAsync(player.UserId.."-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)]
- 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.Coins.Value = player.leaderstats.Coins.Value + 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")
- 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.Coins.Value = player.leaderstats.Coins.Value + reward
- DataStore:SetAsync(player.UserId.."-dailyReward",os.time())
- connection:Disconnect()
- end
- end)
- end
- local player_data
- pcall(function()
- player_data = DataStore:GetAsync(player.UserId.."-Coins")
- end)
- if player_data ~= nil then
- -- Player has saved data, load it in
- coins.Value = player_data
- else
- -- New player
- coins.Value = defaultCash
- end
- game.Players.PlayerRemoving:Connect(function(player)
- pcall(function()
- DataStore:SetAsync(player.UserId.."-Coins",player.leaderstats.Coins.Value)
- end)
- print("Saved points")
- end)
- end)
- --[[game.Players.PlayerRemoving:Connect(function(player)
- local data
- pcall(function()
- data = DataStore:GetAsync(player.UserId.."-dailyReward")
- print("Getting the Data")
- end)
- if data == nil then
- -- New player
- pcall(function()
- local timeVal = os.time()
- DataStore:SetAsync(player.UserId.."-dailyReward")
- end)
- print("Saved because you are a new player")
- end
- end)--]]
- -- Next script; "Points Data Save"
Add Comment
Please, Sign In to add comment