Why is this breaking the if statement?

if v.Occupied.Value == false and Used1 == false  and (not Item1) == nil then
				print("Person")
				Used1 = true
				v.Occupied.Value = true
				v.Name = Item1
				v.Count.Value = Num1
			end

I kept removing parts of the if statement and found that (not Item1) == nil is what is not allowing it to pass. Why is this? I check with print statements and it comes out every time as “Cedar Wood”. The reason that check is there is because these are variables being passed as parameters from a server to a client, and sometimes not every single one has a value. It’s for an inventory system.

try this script

if v.Occupied.Value == false and Used1 == false  and Item1 then
				print("Person")
				Used1 = true
				v.Occupied.Value = true
				v.Name = Item1
				v.Count.Value = Num1
			end

or

if v.Occupied.Value == false and Used1 == false  and Item1 ~= nil then
				print("Person")
				Used1 = true
				v.Occupied.Value = true
				v.Name = Item1
				v.Count.Value = Num1
			end

This works. If you don’t mind me asking, what is the difference between == and ~=?

== returns true if the given parameters are equal but ~= returns true if given parameters are not equal.
Ex:

local a = "A"
local b = "A"
if a == b then
   print("a = b")
else
   print("A ~= b")
end---prints "a == b"

if a ~= b then
   print("a ~= b")
end---prints nothing

Edit: == is like checking if it’s equal and ~= is checking if it’s not equal

Alright makes sense. Thank you for the explination!

1 Like

If you don’t mind, I have one more bug in the code that I don’t know how to fix.
On the server, local Num3 = math.random(1,5) is referenced. Then, it is used as a parameter in a remote function to the client game.ReplicatedStorage.Inventory.AddItem:FireClient(player,Num1,Item1,Num2,Item2,Num3,Item3,Num4,Item4) On the client, Num3 is registered as nil. local function FillSlot(Num1,Item1,Num2,Item2,Num3,Item3,Num4,Item4)

is it only Num3 or few others also registering as nil ?

Only Num3 is being affected by this

Because you are essentially checking if false is equal to nil, which is not true. you should only do not item1 in this scenario, which will check if its true that its false.

You can find this info here which should explain what it does to the code.

If you want to get a specific condition, you use == and ~= as not condition will check if the item is false or nil, which will not help you when you are trying to check for stuff.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.