Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to find class of non-instance values?

Is there a way to get the class/type name of things like BrickColors, Vector3s, etc.

Reason: I usually put assertions in my functions to check that an arguments that it is called with are valid. For Instances, I can check the ClassName property of it, or use the IsA method, but for other things, I cannot.

Example:

function SetColor (Part, Color)
	assert(type(Part) == "userdata" and part:IsA("BasePart), "bad argument #1 to \'SetColor\' (BasePart expected) ") -- This would work
	assert(type(Color) == "userdata" and Color:IsA("BrickColor"), "bad argument #2 to \'SetColor\' (expected BrickColor)") -- Does not work as BrickColor does not have the IsA Method
	assert(type(Color) == "userdata" and Color.ClassName == "BrickColor", "bad argument #2 to \'SetColor\' (expected BrickColor)") -- Does not work as BrickColor does not have the property ClassName
	part.BrickColor = BrickColor
end

This is just an example, and not a real use case.

I know I could use something like

function SetColor (part, Color)
	-- assert part is part
	local a, b = pcall(function() part.BrickColor = Color end)
	if not a then error("bad argument #2 to \'SetColor\' (" .. b:match("([^%(]*)%)$") .. ")") end
end 

But it does not seem that efficient.

1 comment

0

Nevermind: just remembered that Roblox has a function called typeof which does this.

1 answer

I just remembered the method for doing this, `typeof``. This function returns the type of object provided.

print(typeof(Vector3.new())

Vector3

This discussion is preserved as it was. New answers, comments, and votes are closed.