Attempt to perform arithmetic (add) on number and nil

Server:

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

But what is appleAmount used for (in the module) as I can’t see it being used in the scripts provided (unless I’m blind)

Without += works = 8e6b1bd53e83681db160390e637e555f3b95fab0

With += error e7f530bfe49f625d990d00bb305ee3408b4321d8

local remainingApple = appleValue.Value + appleAmount.

So I guess use that if it works and the other doesn’t not sure why though

It says

appleValue.Value + appleValue

In the code you provided

Yeah but then people can’t gain any value which is will stuck at 0 forever, since when you’re collecting it should has + instead just =

I fixed it since I made the mistake, it should’t be the 2 same values at all XD

So it works now?

30 happy roblox

No, I wish if it was worked, still the same issue but at least now it prints appleAmount(15) and the players apple value, but still broken. For example you’re only able to collect the 1 apple, you’re getting + X value from module and it being broken without any reason with the error. So you should reset or just rejoin which is makes me confused.

Can you put the updated code please

Module:

local module = {
addApple = function(appleAmount, appleValue)
		if appleAmount ~= nil and appleValue ~= nil then
			local remainingApple = appleValue.Value + appleAmount
			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)

Well remainingapples will never be less than 0 because you are adding 15 apples

So can I please see the applesCollected function

That’s why it should be always > 0 so it’s fine

Can I please see the applesCollected

Your problem is here, looks like you’re adding appleValue which seems to be an Instance to that same isntance’s value, did you mean


local remainingApple = appleValue.Value + appleAmount

Pretty sure the guy before said this.

It could also be the applesCollected function, since a number + Instance would be nil, but not sure what that function does

Why don’t you always do

appleValue.Value = remainingApple

Yes we have got that issue fixed the new code is a few posts above your post

2 Likes

‘’’
local function applesCollected(appleValue)
local leftOverApple = 0
local newApple = 1
local loopStartTime = tick()
repeat
leftOverApple = remainingApple
if leftOverApple >= 0 then
local continueApple = true
if appleValue ~= nil then
continueApple = false
end
if continueApple == true then
newApple = newApple + 1
remainingApple = leftOverApple
else
remainingApple = 0
leftOverApple = 0
appleValue.Value = newApple
break
end
else
appleValue.Value = remainingApple
break
end
until leftOverApple < 0 or leftOverApple < 0
local loopAfterTime = tick()
local totalLoopTime = loopAfterTime - loopStartTime
if totalLoopTime >= 0.5 then
warn("Apple loop time above 0.5 seconds: " … tostring(totalLoopTime))
end
end
‘’’