Check class of variable (if that class is built-in)

Its the second day of 2020! :partying_face: (Depending on your time zone.)

Anyways, I have a problem here. How can I check the class type of a variable, if that variable is a built-in Roblox class?

I searched on the web for a solution, and I found that getmetatable can be used to get the class type of a variable. But, that only works for vanilla Lua, and not Roblox Lua. When I try getmetatable on a Roblox built-in class (Vector3, UDim2, CFrame, etc.), it return a string “the metatable is locked”.

So the metatable is locked :sweat: (whatever that means). Is it that Roblox have set the built-in class to private? (I’m a beginner at OOP, so I don’t know if it’s possible to do that. If I’m wrong, correct me please :slight_smile:.) And, are there anyways to solve this?

1 Like

if u want u can use typeof() to get a string of the variable (example: bool)

2 Likes

Huh. I guess I didn’t research enough :sweat_smile:

Quick question: what is the difference of type and typeof?

1 Like

type is for Things in Lua, but don’t things in Roblox Globals/Locals, typeof get Globals/Locals of Lua and Globals/Local of Roblox

1 Like

type() is native to Lua, thus it does not recognize Roblox data types.

print(type(CFrame.new())) → userdata
print(typeof(CFrame.new())) → CFrame
1 Like

So typeof is essentially a Roblox function, but not a Lua function? Oh wow that’s interesting :smile:. That explains why I couldn’t find anything about typeof on the web. Because I searched for “check type of variable lua” instead of “check type of variable roblox”.

Oh also, I found this article about the release of typeof.

Thanks for the information @Spooce, and also @pro_developer213. :slightly_smiling_face:

2 Likes