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)
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