Attempt to perform arithmetic (add) on number and nil

Hello! Basically, I created a module that works fine, but what is wrong with the server side?

Module:

local module = {
addApple = function(appleAmount, appleValue)
		if appleAmount ~= nil and appleValue ~= nil then
			local remainingApple = appleValue.Value + appleValue
			if remainingApple > 0 then
				applesCollected(appleValue, remainingApple)
		else
				appleValue.Value = remainingApple
		end
	end
end
}
return module

Server:

rEvents.addApple.OnServerEvent:connect(function(plr)
	local appleAmount = 15
	local appleValue = plr.Apples
	plr.Apples.Value  += module.addApple(appleAmount, appleValue)
end)

plr.Apples is probably nil you should re check that

This is the problem, you are adding a Instance to the same Instances Value.

1 Like

It can’t be nil since I already checked and used print for it, the thing is about + on it

The thing is about server script, if I use plr.Apples.Value = module.addApple it’s works without any problem but it won’t then add the value that I needs, same with - or *

As I said, appleValue is a Instance, you are trying to add it’s Value to the appleValue Instance. I think you meant to put

local remainingApple = appleValue.Value + appleAmount

If you checked the module script it can’t be instance since it uses .Value on the end, read it right.

Take your own advice. I believe if someone points something out, you are supposed to check it?

appleAmount = ‘15’ on the server side, appleValue = plr.Apples, but in the end on module ‘.Value’ makes it: plr.Apples.Value so mainly appleValue.Value = plr.Apples.Value

Ok, look I’m not going to sit here for an hour just fighting about this. appleValue is an INSTANCE. you are try to add ITS VALUE to THE INSTANCE ITSELF.

So you’re trying say is plr.Apples.Value = instance? Since when?..

Right here, you declare it as the INSTANCE of Apples under the Player.

Right here, you said it equals the Instance of Apples Value PLUS the Instance of Apples

If you checked the MODULE SCRIPT it makes already to plr.Apples.Value. So I don’t see any problems with it.

Okay, I’m done, read what I quoted and figure it out yourself. Also, ask anyone, if they know how to script and read it, they will point out the exact same thing I did.

I already spent over 30 minutes to figure out what the problem is, even the print shows everything fine, and there are no ideas why the error appears.

So in the module you have’s
applesCollected(appleValue, remainingApple)

It’s should be:

applesCollected(appleValue.Value, remainingApple)

As I said before it’s not the instance if you’re reading it right, since the module script checking for it.

local remainingApple = appleValue.Value + appleAmount

As I said before the module script works FINE, the only problem is server side one, since if I put plr.Apples.Value = module.addApple(appleAmount, appleValue) it works fine without any errors, but if I’m trying add the value for example: plr.Apples.Value += module.addApple(appleAmount, appleValue) it won’t work well. I just don’t understand how to add values from modules, it’s the thing.

How much I has apples right now:

Print from module:

Error:

Check the screenshots, as I said IT WORKS PRETTY FINE with the module script, the problem is server script.

All values are works and and there are no the nil thing, but when I’m trying plr.Apples.Value + or -, or * then the script stopped work.