local DataStore = game:GetService("DataStoreService"):GetDataStore("DailyReward")

local hourWait = 24

local possibleRewards = {10,10,10,10,10,15,15,15,20,20,10,15,20,50,10,25,20,25,15,10,10}

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 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

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:GetAsync(player,UserId.."-daily reward")
    end)
	print ("Saved because you are a new player")
end

end)--]]

1 comment

0

Where exactly is it erroring?