In addition to DigitalVeer's answer about the "type" function, you have these options:
-ClassType property (for Roblox objects)
-You can try to access a property without fear of an error using pcall:
function HasProp(obj, prop)
local success, value = pcall(function() return obj[prop] end)
return success
end
function TryGetProp(obj, prop)
local success, value = pcall(function() return obj[prop] end)
if success then
return value
else
return nil
end
end
function WhatIs(obj)
if type(obj) == "userdata" then
return TryGetProp(obj, "ClassName") or "userdata"
end
return type(obj)
end
function MightBeVector3(obj)
return type(obj) == "userdata" and HasProp(obj, "X") and HasProp(obj, "Y") and HasProp(obj, "Z")
end
print(WhatIs(Vector3.new()))
print(WhatIs(Instance.new("Part")))
print(MightBeVector3(Vector3.new()))
print(MightBeVector3(CFrame.new()))
print(MightBeVector3(Instance.new("Part")))
You could further improve on the "WhatIs" and "MightBeVector3" functions as desired. ex, if you wanted to make sure that MightBeVector3 didn't return true on a CFrame, you could add and not HasProp(obj, "p"), since CFrames have a ".p" property.
3 comments
Ah, thanks. I have seen this before but since I never use it I forgot about it.
Your types are wrong. Types are lowercase. You cannot determine if something is a Vector3.
typewill report"userdata".I didn't realize that, sorry. I edited to fix it.