Weird Type Warning

Hi,

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

If i copy the whole script and try it, it gives no error. What is p in your script? I guess that could cause the error

1 Like

--!strict

a folder called placeholder.

However thats unrelated to the issue.

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.

It just prints Instance

and I’m not entirely sure what you mean here.

Nevermind, If it’s got an instance it won’t help, but also that means it should work.

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!")
1 Like

Ah, thats why, thanks.

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