How detect - object or string

I have script which can receive 2 data types - string or object. How I can detect when this data is string, and when data is object?

For strings, you can try using typeof(Object) == "string", for instances you can use typeof(Object) == "Instance"

2 Likes

Don’t use typeof when type checking primitive value types, it’s far slower than Lua’s native global function type which can achieve the same task & return the same result. The Roblox exclusive typeof should only be used to type check Roblox’s custom userdata values, i.e; Color3, Vector3, CFrame3 etc.

It might be, but typeof is more intuitive since it doesn’t just return “userdata” for some types which may be confusing to newer developers. It’s for the conciseness.

(Also, the performance benefit is likely negligible)

typeof() supports luau datatypes which can prevent undefined behavior

local Color = Color3.new()
print(type(Color)) --userdata

This isn’t “undefined” behavior.