so I’m trying to have my code look for an item, and create one if it cant find it, but when in Script Editor, it gives me a strange warning that I dont really understand for his code.
Warning: Expected type table, got 'Instance | Folder' instead
Why does this happen?
local item = p:FindFirstChild("items") or Instance.new("Folder")
-- if 'items' is nil, it will create a new Folder for that purpose
-- so basically:
-- nil or Folder = Folder
-- Instance or Folder = Instance
item.Name = "items" -- These lines have the error
item.Parent = p
So my guess is that the type declaration for your variable items is as a table, possibly because it has multiple arguments for its value.
Try printing typeOf(item) to check, and then rearranging the ‘or’ statement to see if that helps.
if you are sure "items" is a folder you could just set the type to Folder, otherwise Instance. Or types usually need to be resolved before use. This case may be a bug since Name and Parent both perfectly overlap.
local item: Instance = p:FindFirstChild("items") or Instance.new("Folder")
If you need items to be a folder then you can use an assert to be extra sure
local item = p:FindFirstChild("items") or Instance.new("Folder")
assert(item:IsA("Folder"), "Item must be a folder!")