Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

A way to find out if something is an object?

Asked by 8 years ago

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

Log in to vote
1
Answered by 8 years ago

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

Ad

Answer this question