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?)
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.
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)