This is because the entire Roblox engine is sandboxed. I’m not gonna go into detail, but basically, there are metatable proxies hooked to everything that perform checks internally and just do different stuff than your regular tables.
You can recreate this yourself:
local a = {1, 2}
setmetatable(a, {
__index = function(self, k)
local v = rawget(self, k)
if v == nil then
error(string.format([["%s" doesn't exist]], k))
end
return v
end
})
print(a[1])
print(a.b) --this will now error
To fix your problem, you have to use the :FindFirstChild() method. It will return nil if the instance isn’t found instead of erroring.
Thanks for explaining __index can be a function, I rarely use this and almost forget that! And :FindFirstChild() works very well, now I know the right steps to do this. Thx again!