"attempt to compare nil and number", even though nil is a number

Hello,

I’m currently trying to compare a number and a number to check if the player has enough currency to purchase the item. However, I’m getting the error “attempt to compare nil and number”. Here’s my code for my LocalScript:

local itemName = script.Parent.ItemName.Value
local currencyType = script.Parent.CurrencyType
local currentAmount

if currencyType.Value == "Tokens" then
	currentAmount = game.ReplicatedStorage:WaitForChild("Functions").GetCurrency:InvokeServer("Tokens")
	print("T$: " .. currentAmount)
elseif currencyType.Value == "Bucks" then
	currentAmount = game.ReplicatedStorage:WaitForChild("Functions").GetCurrency:InvokeServer("Bucks")
	print("B$: " .. currentAmount)
end
	
if (tonumber(currentAmount) >= game.ReplicatedStorage:WaitForChild("Functions").GetPrice:InvokeServer(itemName)) then
	game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("BuyItem"):FireServer(itemName, currencyType.Value)
end

Here’s my Server Script:

events.BuyItem.OnServerEvent:Connect(function(player, itemName, currencyType)
    if player.Currency[currencyType].Value >= ITEM_PRICES[itemName] then
        player.Currency[currencyType].Value = player.Currency[currencyType].Value - ITEM_PRICES[itemName]

        player.Items[itemName].Value = true
    end
end)

Thanks,
p0s_0.

1 Like

Okay I figured it out, currentAmount is = to nil because you never set it to anything and you originally left it blank.

2 Likes

currentAmount is set in the if statement below the variable being set, according to currencyType.

1 Like

Oh I see, which if statement is giving the error?

1 Like
if (tonumber(currentAmount) >= game.ReplicatedStorage:WaitForChild("Functions").GetPrice:InvokeServer(itemName)) then
	game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("BuyItem"):FireServer(itemName, currencyType.Value)
end

This if statement is giving the error.

1 Like

That means the GetCurrency RemoteFunction is returning nil

1 Like

Are you certain the InvokeServer is returning something to put in currentAmount? What is it in RemoteFunction?

1 Like

I checked with print statements, and it is actually returning the currency amount.

1 Like

You’re trying to change currentAmount to a number, which you’ve set to as an InvokeServer function. earlier in your code.

1 Like

Yes I am, I checked with print statements. Here’s the code:

game.ReplicatedStorage:WaitForChild("Functions"):WaitForChild("GetCurrency").OnServerInvoke = function(plr, currencyType)
    if currencyType == "Bucks" then
        return tonumber(plr:WaitForChild("Currency"):WaitForChild("Bucks").Value)
    elseif currencyType == "Tokens" then
        return tonumber(plr:WaitForChild("Currency"):WaitForChild("Tokens").Value)
    end
end

These values are IntValues. I was using tonumber originally to see if that was somehow the issue, however, it wasn’t.

1 Like

You can’t use :InvokeServer() with tonumber

2 Likes

If you’re pulling these stats from values somewhere, try checking to see if the value of the stats is <=(less than or equal to) the required amount.

2 Likes

I’ve gotten rid of the tonumber statements, however the errors persist.

1 Like

Are you able to send me the line of error?

1 Like
if (currentAmount >= game.ReplicatedStorage:WaitForChild("Functions").GetPrice:InvokeServer(itemName)) then
1 Like

Please do paste the error message if you can, again.

1 Like

“attempt to compare nil and number”

1 Like

Are you making sure you are returning a number in the GetPrice function? Can we see the GetPrice function code

1 Like

Yes, it’s an IntValue. I checked.

1 Like

Again, you’re trying to compare an intvalue to a function.

try something like:
if currentAmount >= 9999
“9999” is a default number to compare the integer to.

2 Likes