Xxx is not a valid member of xxx

if A is not a valid member of B, why I can’t get a ‘nil’ from B.A, but get an error?

local a = {1, 2}
print(a.b)  ----- result nil

do same thing in rbx

print(game.Workspace.notInWorkSpace)

----- result:
----- notInWorkSpace is not a valid member of Workspace "Workspace" 

I found the problem in my If-else code

if child.family and child.family == 'zombie' then
    doSomeThing()
end

I imagined that if child dose not have attribute ‘name’, the former condition should be nil and the whole condition will fail, but an error occurred.

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.

1 Like

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!

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