Attempt to perform arithmetic (add) on nil and number

Basically just trying to add a certain amount to the players Credits every 5 minutes depending on the players job. I’m getting an error that says Attempt to perform arithmetic(add) on nil and number.
When the 5 minutes is up and the timers value is set to 0 its supposed to add credits to the player based on the amount of income their job provides.

Code:

local PaycheckWaitTime = 300--(5 minutes)
	
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local EventsFolder = ReplicatedStorage.Events
	
	
	
	while wait(1) do
		local PlayerData = Player:FindFirstChild("PlayerData")
		local PlayerStats = Player:FindFirstChild("PlayerStats")
		
		local PlayerJob = PlayerData.Job.Value
		local PlayerJobFolder = ReplicatedStorage.Jobs:FindFirstChild(PlayerJob)
		local Amount = ReplicatedStorage.Jobs:FindFirstChild(PlayerJobFolder.Salary.Value)
		
		PaycheckTime.Value = PaycheckTime.Value - 1
		if PaycheckTime.Value == 0 then
			PlayerStats.Credits.Value = PlayerStats.Credits.Value +Amount
			PaycheckTime.Value = PaycheckWaitTime
		end
	end
	
end)

image
image
image
In this case, if I chose the farmer job, I should be making $45 every 5 minutes.

1 Like

It means you’re trying to add a number to a value that doesn’t exist. Check the value of the Credits object, and set it to 0 if it’s empty.

1 Like

My credits value is currently at 500 and I’m still getting the same error.

I think error is here:

PlayerStats.Credits.Value = PlayerStats.Credits.Value +Amount

Check Amount value and PlayerStats.Credits.Value . Try to print them when PaycheckTime = 0

They shouldn’t be a nil

1 Like

50 is the salary for the job I chose
500 is the amount of credits I currently have. The error is no longer showing but its not adding to the amount of credits.

Updated Code:

local PaycheckWaitTime = 300--(5 minutes)
	
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local EventsFolder = ReplicatedStorage.Events
	
	
	
	while wait(1) do
		local PlayerData = Player:FindFirstChild("PlayerData")
		local PlayerStats = Player:FindFirstChild("PlayerStats")
		
		local PlayerJob = PlayerData.Job.Value
		local PlayerJobFolder = ReplicatedStorage.Jobs:FindFirstChild(PlayerJob)
		local Amount = PlayerJobFolder.Salary.Value
		
		PaycheckTime.Value = PaycheckTime.Value - 1
		if PaycheckTime.Value == 0 then
			print(Amount)
			print(PlayerStats.Credits.Value)
			PlayerStats.Credits.Value = PlayerStats.Credits.Value +Amount
			PaycheckTime.Value = PaycheckWaitTime
		end
	end
	
end)
PlayerStats.Credits.Value += Amount

try this istead this:

PlayerStats.Credits.Value = PlayerStats.Credits.Value +Amount
1 Like
local PlayerJobFolder = ReplicatedStorage.Jobs:FindFirstChild(PlayerJob)
local Amount = ReplicatedStorage.Jobs:FindFirstChild(PlayerJobFolder.Salary.Value)

The paycheck is now working, the error was in the Amount.

Updated Code:

local PlayerJobFolder = ReplicatedStorage.Jobs:FindFirstChild(PlayerJob)
local Amount = PlayerJobFolder.Salary.Value

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.