ServerScriptService.Shop:18: attempt to perform arithmetic (sub) on number and nil?

I am having a issue where its say ServerScriptService.Shop:18: attempt to perform arithmetic (sub) on number and nil on my shop gui!

local serverStorage = game:GetService("ServerStorage")

local buyRemote = game:GetService("ReplicatedStorage").Events.BuyItem

local itemCosts = {
	["NormalPush"] = 30,
	["Medium Push"] = 60,
	["UltraPush"] = 80
}



buyRemote.OnServerEvent:Connect(function(player,item)
	local playerPushs = player.leaderstats.Pushs

	if playerPushs == nil and item == nil and itemCosts[item] == nil and playerPushs.Value == nil and playerPushs.Value < itemCosts[item] then return end

	playerPushs.Value -= itemCosts[item]

	local toolToGive = serverStorage.Tools:FindFirstChild(item)
	toolToGive.Parent = player.BackPack
end)

It means you are trying to subtract something that doesnt exist from a number. Make sure both of your values are numbers.

Im trying to make it so it subtracts the price from any product they choose. Not just one product

Try printing the ‘item’ and see what you get :slight_smile:

It Prints it but I still get the error?

Try to print itemCosts[item] if this won’t work try to use pairs

local Price 
for i, v in pairs(itemCosts) do
      if i == item then
          something.Value -= v
      end
end

You need to use or instead of and for this if statement.

The error is on this line
playerPushs.Value -= itemCosts[item]

Got an error for price. Unless I have to change it

I’m confuzzled by these lines here, you’re checking if both playerPushs & item are equal to nil which would result in that error…? nil is basically equaling to nothing or not valid

I know. But your cause is here if I’m not mistaken.
if playerPushs == nil and item == nil and itemCosts[item] == nil and playerPushs.Value == nil and playerPushs.Value < itemCosts[item] then return end
Because if itemCosts[item] is nil, it will go through, causing an error. For this conditional of yours to work, item needs to be nil, playerPushs needs to be nil, playerPushs.Value needs to be nil, itemCosts[item] needs to be nil, and playerPushs(nil).Value(nil) needs to be less than itemCosts(nil)[item(nil)] which isn’t possible. This if statement does nothing as it currently is.
Change the ands to ors and it should work.

Now the buy buttons not doing anything

Something is probably nil.
Add this above the if line.

print(playerPushs)
print(item)
print(itemCosts[item])
print(playerPushs.Value)
print(playerPushs.Value<itemCosts[item])

Let me know what it outputs or if it errors.

14:26:48.148 Pushs - Server - Shop:17
14:26:48.148 Normal Push - Server - Shop:18
14:26:48.148 nil - Server - Shop:19
14:26:48.149 0 - Server - Shop:20
14:26:48.149 ServerScriptService.Shop:21: attempt to compare number < nil - Server - Shop:21

So when the function is called, it uses the argument “Normal Push”, but in the table it is read as NormalPush with no space in it. Either get rid of the space on the LocalScript where you call the function, or add a space to the itemCosts table.

Ok now it takes the pushs but then it doesn’t give me the tool and does error ServerScriptService.Shop:27: attempt to index nil with ‘Parent’

Sorry I got distracted. It looks like it can’t find the tool in server storage. What does your tools folder look like?

I fixed it! Thanks for helping! :smiley:

1 Like