Distinguishing Userdata Types

Recently I programmed a data-to-string compiler/decompiler, very similar to JSON but mine supports userdata (Vector3, BrickColor, CFrame, Instance etc.) and is less human readable. I needed to be able to find the DataType of any lua_State. Of course the “type” method can be using to distinguish string, number, nil, userdata etc. but there seems to be no conventional way to get the type of userdatas (custom non-lua-native states) :huh:
Here’s what I made that works, its inefficient, and uses the error handler to check if an index can be accessed without raising an exception. But I really shouldn’t need to do this:

local GetDataType
do
	local function DataCheck(data, key) -- Unconventional and inefficient
		return (pcall(function()
			_ = data[key]
		end))
	end
	function GetDataType(data)
		local t = type(data)
		if t ~= "userdata" then
			return t
		end
		if DataCheck(data, "ClassName") then
			return data.ClassName
		elseif DataCheck(data, "y") then
			if DataCheck(data, "z") then
				if DataCheck(data, "components") then
					return "CFrame"
				else
					return "Vector3"
				end
			else			
				return "Vector2"
			end
		elseif DataCheck(data, "X") then
			return "UDim2"
		elseif DataCheck(data, "Scale") then
			return "UDim"
		elseif DataCheck(data, "ClosestPoint") then
			return "Ray"
		elseif DataCheck(data, "Number") then
			return "BrickColor"
		elseif DataCheck(data, "r") then
			return "Color3"
		elseif DataCheck(data, "Value") then
			return "Enumeration"
		end
		error("Invalid argument exception, userdata type fetch has not yet been implemented")
	end
end

I’m just asking for an ordinary method that can be used to get the type of ROBLOX userdatas

2 Likes

If it works, then dont worry about it.

1 Like

Its so slow :unsure:
If you have a massive nested array filled with userdata it lags, if its full of numbers, it doesn’t

1 Like

print(userdata.ClassName)
?

Janthran, that only works if the userdata is an Instance. Tomarty is talking about other types of userdata, like Vector3, Vector2, BrickColor, etc.

Oh, I see.
now that it’s mentioned that is something that could be useful

A wild newproxy(true) has appeared, and borked up your entire system!

obj = newproxy(true)
mt = getmetatable(obj)
mt.__metatable = true
mt.__tostring = “hi u ok”
mt.__index = {hi = “u ok”,wats = “my type”}

o3o

newproxy’s in Rbx.Lua are useless, because you can’t create them on other userdata’s.

In other words, you have to create a new metatable for EVERY userdata you create.

For tables, you just set all metatables to the same metatable. An example: a simple inheritance table. You can’t do that with roblox userdata’s. They are useless.

Oh yeah and __gc also doesn’t run on userdata’s.

1 Like