Attempt to perform arithmetic (sub) on number and nil

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I basically attempted to script a “each 5 minutes” reward (this reward is only granted once the player rejoins). The currency is called “inputs”.

  2. What is the issue? Include screenshots / videos if possible!
    This works perfectly fine with one player, but as soon as I test the game with two players, only one of them always gets the reward while the other player gets nothing. The script outputs “attempt to perform arithmetic (sub) on number and nil” on line 76. The script will be shown below.

  3. What solutions did you find?
    All I found were other issues regarding “attempt to perform arithmetic (sub) on number and nil”.

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local FreeRewardsDataStore = DataStoreService:GetDataStore("FreeRewards")

local WaitMinutes = 5
local WaitSeconds = WaitMinutes * 60

local PossibleRewards = {50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 
	100, 100, 100, 100, 100, 100, 100, 
	200, 200, 
	500}
--[[ Reward chances
50 inputs = 75%
100 inputs = 17.5%
200 inputs = 5%
500 inputs = 2.5%
]]
--while true do
--	wait(0.5)
--	print(PossibleRewards[math.random(1, #PossibleRewards)])
--end

local FreeRewardEvent = game.ReplicatedStorage.InputStuff.FreeRewardEvent

Players.PlayerAdded:Connect(function(plr)
	local LeaderStats = plr:WaitForChild("leaderstats")
	local InputsLeft = LeaderStats["Inputs Left"]
	local UserID = plr.UserId
	
	local MBNR = Instance.new("IntValue")
	MBNR.Name = UserID.."-MinsBeforeNextReward"
	MBNR.Parent = ReplicatedStorage.InputStuff.MBNR
	
	local timeNow = os.time()
	
	local data
	
	pcall (function()
		data = FreeRewardsDataStore:GetAsync(UserID.."-Free_Inputs")
	end)
	
	if data ~= nil then
		-- returning player
		print(plr.Name.." joined as a returning player!")
		local timeSinceLastClaim = timeNow - data --seconds
		
		if (timeSinceLastClaim / WaitSeconds) >= WaitMinutes then
			local Reward = PossibleRewards[math.random(1, #PossibleRewards)]
			FreeRewardEvent:FireClient(plr, WaitMinutes, Reward)
			
			local connection
			connection = FreeRewardEvent.OnServerEvent:Connect(function(Claimer)
				if Claimer == plr then
					print(plr.Name.." recieved a free reward of "..Reward.." inputs.")
					
					if ReplicatedStorage.BannedStatus[UserID].Value == true then
						InputsLeft.Value = InputsLeft.Value + Reward
					else
						InputsLeft.Value = InputsLeft.Value + Reward + 1
					end
					
					FreeRewardsDataStore:SetAsync(UserID.."-Free_Inputs", os.time())

					connection:Disconnect()

				end
			end)
		end	
		
		
		
	else
		--new player
		print(plr.Name.." joined as a new player!")
		local timeSinceLastClaim = timeNow - data
			
		local Reward = PossibleRewards[math.random(1, #PossibleRewards)]
		FreeRewardEvent:FireClient(plr, WaitMinutes, Reward)

		local connection
		connection = FreeRewardEvent.OnServerEvent:Connect(function(Claimer)
			if Claimer == plr then
				print(plr.Name.." recieved a free reward of "..Reward.." inputs.")
				InputsLeft.Value = InputsLeft.Value + Reward + 1
				FreeRewardsDataStore:SetAsync(UserID.."-Free_Inputs", os.time())

				connection:Disconnect()

			end
		end)
		
		
	end
	
end)

--Players.PlayerAdded:Connect(function(plr)
--	FreeRewardsDataStore:SetAsync(plr.UserId.."-Free_Inputs", 0)
--end)

-- Scraped
--[[Players.PlayerRemoving:Connect(function(plr)
	local data
	local UserID = plr.UserId
	
	pcall(function()
		data = FreeRewardsDataStore:GetAsync(UserID.."-Free_Inputs")
	end)
	
	if data  == nil then
		
		pcall (function()
			local timeVal = os.time()
			FreeRewardsDataStore:SetAsync(UserID.."-Free_Inputs")
			print(plr.Name.."'s free rewards data saved.")
		end)
	end
	
end)
]]

Line 76: local timeSinceLastClaim = timeNow - data

Line 76 runs if data is nil, therefore its timeNow - nil which would error.
Removing that line should fix the problem and have your script run perfectly fine.

1 Like
local timeSinceLastClaim = timeNow - data or os.time()
1 Like