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)
if (tonumber(currentAmount) >= game.ReplicatedStorage:WaitForChild("Functions").GetPrice:InvokeServer(itemName)) then
game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("BuyItem"):FireServer(itemName, currencyType.Value)
end
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.