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

Checking Property Type?

Asked by 9 years ago

How would you check the type of a property (i.e. if a string or number or Vector3 value)

Example:

local x = Vector3.new(0, 1, 5)

if x.PropertyType == "String" then
    print("x is a string")
elseif x.PropertyType == "Vector3" then
    print("x is a Vector value")
end

Obviously this is just an example and I need to know how to actually do it.

Sorry if this doesn't follow all guidelines for posting questions.

2 answers

Log in to vote
1
Answered by 9 years ago

Type

Roblox has a built-in method called type. This allows you to check the type of data values. However, if you try it on a Vector3, it will return 'userdata'.

For example:

print(type("Hello!"))
--string
print(type(3))
--integer/number
print(type(true))
--boolean

Using this, you can check other datavalues too.

local x = Vector3.new(0, 1, 5)

if type(x) == "string" then
    print("x is a string")
elseif type(x) == "userdata" then
    print("x is a userdata")
end

Also a list of data types can be found here:

http://wiki.roblox.com/index.php?title=Data_Types

1
Ah, thanks. I have seen this before but since I never use it I forgot about it. VariadicFunction 335 — 9y
0
Your types are wrong. Types are **lowercase**. You cannot determine if something is a Vector3. `type` will report `"userdata"`. BlueTaslem 18071 — 9y
0
I didn't realize that, sorry. I edited to fix it. DigitalVeer 1473 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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) --returns true if "obj" has the property "prop"
    local success, value = pcall(function() return obj[prop] end)
    return success
end
function TryGetProp(obj, prop) --returns the specified property of "obj" if it exists, or else nil
    local success, value = pcall(function() return obj[prop] end)
    if success then
        return value
    else
        return nil
    end
end
function WhatIs(obj) --returns the type or ClassName of obj, if available
    if type(obj) == "userdata" then
        return TryGetProp(obj, "ClassName") or "userdata"
    end
    return type(obj)
end
function MightBeVector3(obj) --returns true if obj might be a Vector3
    return type(obj) == "userdata" and HasProp(obj, "X") and HasProp(obj, "Y") and HasProp(obj, "Z")
end
print(WhatIs(Vector3.new())) --userdata
print(WhatIs(Instance.new("Part"))) --Part
print(MightBeVector3(Vector3.new())) --true
print(MightBeVector3(CFrame.new())) --true, since CFrame has X/Y/Z
print(MightBeVector3(Instance.new("Part"))) --false

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.

Answer this question