Daily rewards system data store error

local dailyRewardsDS = game:GetService("DataStoreService"):GetDataStore("DailyRewards")

local hourWait = 0.1

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr 
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats
	
	local timeNow = os.time()
	
	local data
	
	pcall(function()
		data = dailyRewardsDS:GetAsync(plr.UserId .. "-DailyRewards")
		print("Getting data")
	end)
	
	if data ~= nil then
		
		local timeSinceLastClaim = timeNow - data
		
		print("Time since last claim " .. timeSinceLastClaim)
		
		if (timeSinceLastClaim / 3600) >= hourWait then
			
			local reward = 50
			
			local prompt = script.Parent
			
			prompt.Trigged:Connect(function()
				
				print("Reward claimed")
				
				money.Value = money.Value + reward
				
				dailyRewardsDS:SetAsync(plr.UserId .. "-DailyRewards", os.time())
			end)
			
			print("Player is not elegible right now")
		end		
		
	else
		
		print("New player")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local data
	
	pcall(function()
		data = dailyRewardsDS:GetAsync(plr.UserId .. "-DailyRewards")
		print("Getting the data")
	end)
	
	if data == nil then
		
		pcall(function()
			local timeVal = os.time()
			dailyRewardsDS:SetAsync(plr.UserId .. "-DailyRewards")
		end)
		
		print("Saved because new player")
	end
end)

It is not allowing me to subtract the current time from the data, anyone know why?

Error line: timeNow - data

Add a print(data) to check what the data actually saves as.
(Also this should go in the “Scripting Support” category)

1 Like

I think you can simplify your code. Is Players.PlayerRemoving really necessary? The code I’m posting works, so try building off of it.

local dailyRewardsDS = game:GetService("DataStoreService"):GetDataStore("DailyRewards")

local hourWait = 0

game:GetService("Players").PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats
	
	local timeNow = os.time()
	local data
	
	pcall(function()
		data = dailyRewardsDS:GetAsync(plr.UserId .. "-DailyRewards")
	end)
	
	--[[
		If no data is found, player is probably new to the game, so
		give them the chance to claim their reward immediately.
		This part is to be double checked.
	]]
	local lastClaimTime = (data ~= nil) and (timeNow - data) or 0
	print("Time since last claim: " .. lastClaimTime .. " s")
	
	if lastClaimTime == 0 or (lastClaimTime / 3600) >= hourWait then
		local reward = 50
		local prompt = script.Parent
		
		-- Clear up the memory by disconnecting already used events.
		local event; event = prompt.Triggered:Connect(function()
			print("Reward claimed")
			money.Value = money.Value + reward
			
			local success, err = pcall(function()
				dailyRewardsDS:SetAsync(plr.UserId .. "-DailyRewards", os.time())
			end)
			event:Disconnect()
			if not success then
				warn("Error occured while saving last claim time: ", err)
			end
		end)
	end
end)