If Statement not Detecting nil

Hi all,
My problem is a bit odd but just hang in there.

Long story short, I have an if statement that checks a value in a table. If the value is an empty string or nil then it sets a separate value to an empty string, if it isn’t, it sets the separate value to the string value of the table index.

This… should be fine right? Well… no. For some reason I keep getting messages from the script stating the value it received is nil, even when the if statement should have checked for this. I don’t know if this is a bug or something but its really confusing as to why this is happening.

To clarify: heres the code I’m using.

if _G.Slot[i] == ("" or nil) then--_G.Slot is a global table variable, "i" is a value from the for loop this sits inside
	script.Parent:FindFirstChild(i).ImageLabel.Image = ""
else
    script.Parent:FindFirstChild(i).ImageLabel.Image = TowerIds[2][table.find(TowerIds[1], _G.Slot[i])]
    --Error occurs here, "Invalid argument #3 (String expected, got nil)
end

Also: Quick note, this is all in a pcall function so while its not a terribly bad problem, it is still concerning as to why this is happening and I do believe it is effecting the game negatively as there are several problems in this script with similar issues.

Does anybody know why this is happening or a possible fix for it?

Thank you for your time.

That part of the script is wrong, it will always be checking for “” and not nil. Why?
Well, because “” is not nil neither false

Since you want to do this you need to write:

local Slot = _G.Slot[i]
if Slot == "" or Slot == nil then

end

Another thing I want to say (this is off topic), you should never use _G in Roblox as you never know which script runs first and it might cause issues.

1 Like

Thanks that should help! About the _G though, I didn’t want to use a bunch of passing things from the client to server and back again, what would be a similar way to share these values but without server-client communication and without risking issues occurring?

ModuleScript is recommended over _G and shared. If you put the Module in ReplicatedStorage for example, the client is able to use the code in that module (changes made in the module doesn’t replicate, each client gets a copy of the module, the server has its own).

If you don’t want the ModuleScript to replicate, a great place to put it is in ServerStorage. For example:

ModuleScript (In ServerStorage):

local Slots = {}
return Slots

Script:

local Slots = require(game.ServerStorage.ModuleScript)
local Slot = Slots[i]
if Slot == "" or Slot == nil then

end
1 Like