Daily Reward system not working

Hello,

I am making a Daily Reward System for my game, I tried following AlvinBlox’s tutorial on how to make one, everything was going well until I tested my game, and got no results. Here is the Script and LocalScript I wrote.

Script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local TimeDataStore = DataStoreService:GetDataStore("TimeDataStore")

local RewardDataStore = DataStoreService:GetDataStore("RewardDataStore")
local hourTime = 0.00001
local Reward = 900
local EventGive = game:GetService("ReplicatedStorage").RewardEvents.GiveReward
local EventSave = game:GetService("ReplicatedStorage").RewardEvents.SaveReward

local function playerAdded(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local seconds = Instance.new("IntValue")
	seconds.Name = "Seconds"
	seconds.Value = 900
	seconds.Parent = leaderstats

	if seconds.Value > 0 then
		seconds.Changed:Connect(function()
			if seconds.Value < 1 then
				seconds.Value = 0
			end
		end)
	end
	
	if seconds.Value < 999999999999 then
		seconds.Changed:Connect(function()
			if seconds.Value > 999999999999 then
				seconds.Value = 999999999999
			end
		end)
	end

	local success, value = pcall(function()
		return TimeDataStore:GetAsync(tostring(plr.UserId))
	end)

	if success and value then
		seconds.Value = value
	else
		seconds.Value = 900
	end

	seconds.Parent = leaderstats

	while true do
		wait(1)
		plr.leaderstats.Seconds.Value = plr.leaderstats.Seconds.Value - 1
	end
	
	local timeNow = os.time()
	local data
	
	pcall(function()
		data = RewardDataStore:GetAsync(plr.UserId)
	end)
	
	if data ~= nil then
		local lastSession = timeNow - data
		
		if (lastSession / 3600) >= hourTime then
			EventGive:FireClient(plr, hourTime, Reward)

			local connection
			connection = EventSave.OnServerEvent:Connect(function(triggerPlr)
				if triggerPlr == plr then
					RewardDataStore:SetAsync(plr.UserId, os.time())
					connection:Disconnect()
				end
			end)
		else
			return nil
		end
	else
		EventGive:FireClient(plr, hourTime, Reward)

		local connection
		connection = EventSave.OnServerEvent:Connect(function(triggerPlr)
			if triggerPlr == plr then
				RewardDataStore:SetAsync(plr.UserId, os.time())
				connection:Disconnect()
			end
		end)
	end
end

local function playerLeaving(plr)
	local success, err = pcall(function()
		TimeDataStore:SetAsync(tostring(plr.UserId), plr.leaderstats.Seconds.Value)
	end)

	if not success then
		warn("Failed to save Timer data for player " .. plr.Name .. ": " .. err)
	end
	
	local data
	
	pcall(function()
		data = RewardDataStore:GetAsync(plr.UserId)
	end)
end

Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerLeaving)

game:BindToClose(function()
	task.wait(10)
end)

LocalScript:

local GiveEvent = game:GetService("ReplicatedStorage").RewardEvents.GiveReward
local SaveReward = game:GetService("ReplicatedStorage").RewardEvents.SaveReward
local TextLabel = script.Parent
local Sound = script.Parent.RewardSound

local Player = script.Parent.Parent.Parent.Parent
local Stat = Player:WaitForChild("leaderstats"):WaitForChild("Seconds")

local TS = game:GetService("TweenService")
local TI = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
local Tween = TS:Create(TextLabel, TI, {TextTransparency = 1})
local Tween2 = TS:Create(TextLabel, TI, {BackgroundTransparency = 1})

function rewardReceived()
	TextLabel.BackgroundTransparency = .75
	TextLabel.TextTransparency = 0
	Sound:Play()
	Stat.Value = Stat.Value + 900
	task.wait(5)
	Tween:Play()
	Tween2:Play()
end

GiveEvent.OnClientEvent:Connect(function(lastSession, Reward)
	rewardReceived()
	SaveReward:FireServer()
end)

The code is a bit modified since I made it my way. If you can help me or want more information please reply. (I’m still new to coding so I don’t have ideas on how to fix it.)

In the Server Script, I noticed that you used a while true loop. That also means that anything below that loop will no longer work, because the code will never get to it.

while true do
	wait(1)
	plr.leaderstats.Seconds.Value = plr.leaderstats.Seconds.Value - 1
end
-- This will never run because of the loop above^
local timeNow = os.time()
local data

pcall(function()
	data = RewardDataStore:GetAsync(plr.UserId)
end

I moved it to the bottom, and I got a new error now.

ServerScriptService.Leaderstats.ManageLeaderstat:58: attempt to perform arithmetic (sub) on number and table

Since you’re a beginner (I’m assuming that since you look at simple youtube tutorials), I feel like it’s important for me to also teach you how to deal with a variety of errors.
In this case, the script tried to “perform arithmetic (sub)” (that means do math; sub meaning subtraction), “on number and table”. So you basically did a number subtracted to a table, which does not make sense. It’s like saying 6 - {“Apple”}. Doesn’t work, will never work.

So what should you do?
→ Well, what value did you expect? Since you’re performing math, you know you should have expected a number. Since you’ve received a table instead, you could try and print the table and see if your desired number can be found in there.

Hope this helps!

1 Like