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

A way to find out if something is an object?

I'm trying to make a function that finds out if the argument that's passed is a physical object or not.

Here's the code:

local function IsObject(v)
	return type(v) == "userdata"
end

print(IsObject(workspace.Baseplate)) -- > true

So far so good.

Here's the problem

All physical objects are userdata values. All userdata values, are NOT physical objects. Userdata is ROBLOX Lua specific data structure. That includes data such as: BrickColor, CFrame, Vector3, and more.

Now let's try this again:

local function IsObject(v)
	return type(v) == "userdata"
end

print(IsObject(CFrame.new())) -- > true
print(IsObject(Vector3.new())) -- > true
print(IsObject(BrickColor3.new())) -- > true

See the problem here? Is there a way I can make a function return true ONLY if the argument passed is a physical object?

1 answer

✓ Accepted answer

Valkyrie solutions to the rescue

function IsInstance(o)
	if type(o) == 'userdata' then
		local s,e = pcall(game.GetService, game, o);
		return s and not e
	end;
	return false;
end

Source: BaseLib and DataTypes

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