Attempt to compare number <= nil, however <= is actually >= and nil is a number?

Hello,

My game has been breaking randomly for the past week, and this is another bug that I haven’t been able to figure out. I use a template system for loading items in the Shop. I tried printing the value before purchasing the item and getting an error, and instead of nil, I see “500”, “20”, etc.

Why is this happening? This worked fine a week ago, and without any changes, suddenly stopped working. Here’s my code:

Client:

local itemName = script.Parent.ItemName.Value
local itemNameInCode = script.Parent.ItemNameInCode.Value
local currencyType = script.Parent.CurrencyType
local currentAmount
	
if currencyType.Value == "Bucks" then
	currentAmount = game.ReplicatedStorage:WaitForChild("Functions").GetCurrency:InvokeServer("Bucks")
elseif currencyType.Value == "Tokens" then
	currentAmount = game.ReplicatedStorage:WaitForChild("Functions").GetCurrency:InvokeServer("Tokens")
end
	
local price = game.ReplicatedStorage:WaitForChild("Functions").GetPrice:InvokeServer(itemNameInCode)
	
print(tostring(price)) -- doesn't print nil
-- TODO: Handle return false
if (currentAmount >= price) then
	game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("BuyItem"):FireServer(itemNameInCode, currencyType.Value) -- this errors
end

Server:

funcs:WaitForChild("GetPrice").OnServerInvoke = function(player, itemName)
    for v,i in pairs(ITEM_PRICES) do
        if v == itemName then
			return i
		else
			warn("Item price doesn't exist!") -- i don't see this in output
			return false
        end
    end
end

Thanks,
p0s_0.

1 Like

Hi there,
from the title I am really unable to find out what your issue is, but after looking deeper into this, I see you may be facing a problem when currentAmount is a nil, resulting to such an error.

While you did mark different line as the one you have problems with, I see no reason why would that give you an error, unless the BuyItem is not a RemoteEvent.

There’s also one thing I would like to point out - it’s the fact you use :WaitForChild almost completely everywhere you can. While this is somewhat good way to ensure the Instance is in it’s place before you manipulate with it, it is really bad for performance.

2 Likes

currentAmount seems to return a number, and is not nil. I set currentAmount in the if statement right below the variable.

Could you possibly paste in the error?

Yup, I see that! I was just worried the server might return a nil in some case.

Could you please tell us what the error is?

attempt to compare number <= nil @Metaqione

Can’t really do much with that. Please copy and paste the full error.

I don’t believe the error comes from that line. Isn’t it possible that the error comes from server side?

@BananDev @Metaqione Players.(username).PlayerGui.MainUI.Main.Panels.Shop.AccessoryListing.TinPot.Buy.Click:21: attempt to compare number <= nil

I don’t believe so, I don’t see any errors in the server.

I don’t see anything like that on that line.

Are you sure you are reviewing the correct script?
Aren’t you getting this error on some older version of the script?

Hey, could you show us the GetPrice function in your module?

@BananDev : I am reviewing the correct script, and this is not an older version.
@Metaqione : Look at the original post, it’s below the client script.

My bad, I read that wrong. I thought you had a module script.

Are you sure your ITEM_PRICES table is modeled like the following?

local ITEM_PRICES = {
    Apple = 15,
    Orange = 20
}

I say this because I noticed that you used “for v, i,” which while it makes no difference to the functionality, I think can cause a little confusion.

1 Like

Yes, it is modeled exactly like this.

If you comment out the line, would the error still happen?
This is not going to fix your code by any means, I would just like to be 100% sure the error comes from this line, because it’s very unrealistic that the error comes from this line of code.
If the error’s origin is actually this line, the error shouldn’t be there if you comment that piece of code out.

This might be really weird approach of debugging, but I tend to do this when I am lost and it works very good.

1 Like

Maybe your if condition is returning false both times, leaving currentAmount empty? Put a print statement in both and see what happens.

Edit: both the initial check and in the elseif, that is.

Run this a few times to see if anything changes, since you said this script is working inconsistently. Whenever things just stop working out of nowhere, it’s often that pieces aren’t loading in/changing when you need them to asynchronously, and you’ve just never run into it before.

2 Likes

@Metaqione @BananDev It seems like the error no longer occurs. Thanks for the help! If I could mark 2 posts as solutions, I would.

Well, wait a second there. Read my edit; the chances are that you’ve just stopped running into for now. You want to squash this now while it’s still fresh on your mind, because if you didn’t change anything, it will happen again. Like I said, since it’s am inconsistent error, it’s probably some dependency not loading in. Try doing what I suggested, and test it several times. Even if you don’t get the error again, leave those print statements in, so whenever you do get it again, you’ll see what’s happening. You should be doing everything in your power to debug this while it’s still small.

2 Likes