Is there a way to find if a string found as an instance classname

Here’s an example:

local a = "NumberValue"
local b = "apple"

Instance.new(a,workspace) -- NumberValue added into workspace.
Instance.new(b,workspace) -- error

-- I want to check if the string is exist as an instance classname.
-- If true, return true else return false.

Can anyone help me with this? (Any other solution other than using pcall?)

1 Like

I don’t think there is, other than creating a dictionary of every Class Name you want to allow:

local ValidInstances = {
    Part = true,
    PointLight = true,
    AudioPlayer = true
}

local ok = "Part"
-- true 
print(ValidInstances[ok])

local bad = "orang"
-- nil
print(ValidInstances[bad])

if ValidInstances[someString] then
    -- if "ValidInstances[someString]" is TRUE
    Instance.new(someString) -- no error
else -- otherwise if "ValidInstances[someString]" is NIL 
    warn("Not a valid Class Name!")
end

You could probably put this in a ModuleScript to reduce clutter.

A list of every single Class is available here:

You can’t use some of them with Instance.new, though. They are the ones which have Not Creatable at the top of their respective pages.

If this is for some kind of system to allow players to build stuff in your game, you can probably just hand-pick a few of them instead of adding every single one.

1 Like

I’ll try that, even it’s a waste of time making a long dictionary or an array putting every existing classname. (For something that is important.)

1 Like

You can build that fetching from the latest Roblox API dump using HttpService and a proxy site.

local PROXY -- YOUR PROXY SITE
local HttpService = game:GetService("HttpService")

local api = HttpService:JSONDecode(HttpService:GetAsync(`http://setup.{PROXY}/{HttpService:GetAsync(`http://setup.{PROXY}/versionQTStudio`)}-API-Dump.json`))
local classApi = api.Classes

local classes = {}
for _, c in classApi do
	table.insert(classes, c.Name)
end
print(classes)
1 Like