How do you check if a string is equal to an existing class?

Hello, I’m working on a plugin, and I need to check if the input to a TextBox is equal to a valid class.
How do you do it? I have no ideas and I’m stumped.

It should work like this:

If they typed in something like “BasePart” then it would validate it because the “BasePart” class exists.
If they typed in something like “McDonalds” then it shouldn’t validate it because a “McDonalds” class doesn’t exist.

I know how to do it with classes that are able to be instanced, but how do you make it work with classes that are unable to be instanced?

(My last resort is putting all of the classes into a table)

I don’t know if this would work since I didn’t test it, and it’s probably not the most efficient way, but you could do:

local s, e = pcall(function()
workspace:FindFirstChildOfClass(Input)
end)

if s then
--the class exists
end
1 Like

This might not be the best approach. But maybe this? I don’t think this covers all classes, but is this sufficient?

I got all of the classes from the page, and made it into metatables (you don’t need them though), any string that’s a class that doesn’t exist will return false.

local classes = setmetatable({["ABTestService"] = true, ["Accessory"] = true, ["Accoutrement"] = true, ["AdService"] = true, ["AdvancedDragger"] = true, ["AlignOrientation"] = true, ["AlignPosition"] = true, ["AnalysticsSettings"] = true, ["AnalyticsService"] = true, ["AngularVelocity"] = true, ["Animation"] = true, ["AnimationController"] = true, ["AnimationTrack"] = true, ["Animator"] = true, ["AppStorageService"] = true, ["ArcHandles"] = true, ["AssetService"] = true, ["Attachment"] = true, ["Backpack"] = true, ["BackpackItem"] = true, ["BadgeService"] = true, ["BallSocketConstraint"] = true, ["BasePart"] = true, ["BasePlayerGui"] = true, ["BaseScript"] = true, ["Beam"] = true, ["BevelMesh"] = true, ["BillboardGui"] = true, ["BinaryStringValue"] = true, ["BindableEvent"] = true, ["BindableFunction"] = true, ["BlockMesh"] = true, ["BloomEffect"] = true, ["BlurEffect"] = true, ["BodyAngularVelocity"] = true, ["BodyColors"] = true, ["BodyForce"] = true, ["BodyGyro"] = true, ["BodyMover"] = true, ["BodyPosition"] = true, ["BodyThrust"] = true, ["BodyVelocity"] = true, ["BoolValue"] = true, ["BoxHandleAdornment"] = true, ["BrickColorValue"] = true, ["BrowserService"] = true, ["CacheableContentProvider"] = true, ["Camera"] = true, ["CFrameValue"] = true, ["ChangeHistoryService"] = true, ["CharacterAppearance"] = true, ["CharacterMesh"] = true, ["Chat"] = true, ["ChorusSoundEffect"] = true, ["ClickDetector"] = true, ["ClientReplicator"] = true, ["Clothing"] = true, ["ClusterPacketCache"] = true, ["CollectionService"] = true, ["Color3Value"] = true, ["ColorCorrectionEffect"] = true, ["CompressorSoundEffect"] = true, ["ConeHandleAdornment"] = true, ["Configuration"] = true, ["Constraint"] = true, ["ContentProvider"] = true, ["ContextActionService"] = true, ["Controller"] = true, ["ControllerService"] = true, ["CookiesService"] = true, ["CoreGui"] = true, ["CorePackages"] = true, ["CoreScript"] = true, ["CoreScriptSyncService"] = true, ["CornerWedgePart"] = true, ["CSGDictionaryService"] = true, ["CustomEvent"] = true, ["CustomEventReceiver"] = true, ["CylinderHandleAdornment"] = true, ["CylinderMesh"] = true, ["CylindricalConstraint"] = true, ["DataModel"] = true, ["DataModelMesh"] = true, ["DataModelSession"] = true, ["DataStorePages"] = true, ["DataStoreService"] = true, ["Debris"] = true, ["DebuggablePlugin"] = true, ["DebuggerBreakpoint"] = true, ["DebuggerManager"] = true, ["DebuggerWatch"] = true, ["DebugSettings"] = true, ["Decal"] = true, ["Dialog"] = true, ["DialogChoice"] = true, ["DistortionSoundEffect"] = true, ["DockWidgetPluginGui"] = true, ["DoubleConstrainedValue"] = true, ["DraftsService"] = true, ["Dragger"] = true, ["DynamicRotate"] = true, ["EchoSoundEffect"] = true, ["EmotesPages"] = true, ["EqualizerSoundEffect"] = true, ["EventIngestService"] = true, ["Explosion"] = true, ["FaceInstance"] = true, ["Feature"] = true, ["File"] = true, ["FileMesh"] = true, ["Fire"] = true, ["Flag"] = true, ["FlagStand"] = true, ["FlagStandService"] = true, ["FlangeSoundEffect"] = true, ["FloorWire"] = true, ["FlyweightService"] = true, ["Folder"] = true, ["ForceField"] = true, ["FormFactorPart"] = true, ["Frame"] = true, ["FriendPages"] = true, ["FriendService"] = true, ["FunctionalTest"] = true, ["GamepadService"] = true, ["GamePassService"] = true, ["GameSettings"] = true, ["GenericSettings"] = true, ["Geometry"] = true, ["GlobalDataStore"] = true, ["GlobalSettings"] = true, ["Glue"] = true, ["GoogleAnalyticsConfiguration"] = true, ["GroupService"] = true, ["GuiBase"] = true, ["GuiBase2d"] = true, ["GuiBase3d"] = true, ["GuiButton"] = true, ["GuidRegistryService"] = true, ["GuiLabel"] = true, ["GuiMain"] = true, ["GuiObject"] = true, ["GuiService"] = true, ["HandleAdornment"] = true, ["Handles"] = true, ["HandlesBase"] = true, ["HapticService"] = true, ["Hat"] = true, ["HingeConstraint"] = true, ["Hint"] = true, ["Hole"] = true, ["Hopper"] = true, ["HopperBin"] = true, ["HttpRbxApiService"] = true, ["HttpRequest"] = true, ["HttpService"] = true, ["Humanoid"] = true, ["HumanoidController"] = true, ["HumanoidDescription"] = true, ["ImageButton"] = true, ["ImageHandleAdornment"] = true, ["ImageLabel"] = true, ["InputObject"] = true, ["InsertService"] = true, ["Instance"] = true, ["IntConstrainedValue"] = true, ["IntValue"] = true, ["InventoryPages"] = true, ["JointInstance"] = true, ["JointsService"] = true, ["KeyboardService"] = true, ["Keyframe"] = true, ["KeyframeMarker"] = true, ["KeyframeSequence"] = true, ["KeyframeSequenceProvider"] = true, ["LanguageService"] = true, ["LayerCollector"] = true, ["Light"] = true, ["Lighting"] = true, ["LineForce"] = true, ["LineHandleAdornment"] = true, ["LocalizationService"] = true, ["LocalizationTable"] = true, ["LocalScript"] = true, ["LocalStorageService"] = true, ["LoginService"] = true, ["LogService"] = true, ["LuaSettings"] = true, ["LuaSourceContainer"] = true, ["LuaWebService"] = true, ["ManualGlue"] = true, ["ManualSurfaceJointInstance"] = true, ["ManualWeld"] = true, ["MarketplaceService"] = true, ["MemStorageConnection"] = true, ["MemStorageService"] = true, ["MeshContentProvider"] = true, ["MeshPart"] = true, ["Message"] = true, ["MessagingService"] = true, ["Model"] = true, ["ModuleScript"] = true, ["Motor"] = true, ["Motor6D"] = true, ["MotorFeature"] = true, ["Mouse"] = true, ["MouseService"] = true, ["MultipleDocumentInterfaceInstance"] = true, ["NegateOperation"] = true, ["NetworkClient"] = true, ["NetworkMarker"] = true, ["NetworkPeer"] = true, ["NetworkReplicator"] = true, ["NetworkServer"] = true, ["NetworkSettings"] = true, ["NoCollisionConstraint"] = true, ["NonReplicatedCSGDictionaryService"] = true, ["NotificationService"] = true, ["NumberValue"] = true, ["ObjectValue"] = true, ["OrderedDataStore"] = true, ["PackageLink"] = true, ["PackageService"] = true, ["Pages"] = true, ["Pants"] = true, ["ParabolaAdornment"] = true, ["Part"] = true, ["PartAdornment"] = true, ["ParticleEmitter"] = true, ["PartOperation"] = true, ["PartOperationAsset"] = true, ["Path"] = true, ["PathfindingService"] = true, ["PermissionsService"] = true, ["PhysicsService"] = true, ["PhysicsSettings"] = true, ["PitchShiftSoundEffect"] = true, ["Platform"] = true, ["Player"] = true, ["PlayerEmulatorService"] = true, ["PlayerGui"] = true, ["PlayerMouse"] = true, ["Players"] = true, ["PlayerScripts"] = true, ["Plugin"] = true, ["PluginAction"] = true, ["PluginDebugService"] = true, ["PluginDragEvent"] = true, ["PluginGui"] = true, ["PluginGuiService"] = true, ["PluginManager"] = true, ["PluginMenu"] = true, ["PluginMouse"] = true, ["PluginToolbar"] = true, ["PluginToolbarButton"] = true, ["PointLight"] = true, ["PointsService"] = true, ["PolicyService"] = true, ["Pose"] = true, ["PostEffect"] = true, ["PrismaticConstraint"] = true, ["PVAdornment"] = true, ["PVInstance"] = true, ["QWidgetPluginGui"] = true, ["RayValue"] = true, ["RbxAnalyticsService"] = true, ["ReflectionMetadata"] = true, ["ReflectionMetadataCallbacks"] = true, ["ReflectionMetadataClass"] = true, ["ReflectionMetadataClasses"] = true, ["ReflectionMetadataEnum"] = true, ["ReflectionMetadataEnumItem"] = true, ["ReflectionMetadataEnums"] = true, ["ReflectionMetadataEvents"] = true, ["ReflectionMetadataFunctions"] = true, ["ReflectionMetadataItem"] = true, ["ReflectionMetadataMember"] = true, ["ReflectionMetadataProperties"] = true, ["ReflectionMetadataYieldFunctions"] = true, ["RemoteEvent"] = true, ["RemoteFunction"] = true, ["RenderingTest"] = true, ["RenderSettings"] = true, ["ReplicatedFirst"] = true, ["ReplicatedStorage"] = true, ["ReverbSoundEffect"] = true, ["RobloxPluginGuiService"] = true, ["RobloxReplicatedStorage"] = true, ["RocketPropulsion"] = true, ["RodConstraint"] = true, ["RopeConstraint"] = true, ["Rotate"] = true, ["RotateP"] = true, ["RotateV"] = true, ["RunningAverageItemDouble"] = true, ["RunningAverageItemInt"] = true, ["RunningAverageTimeIntervalItem"] = true, ["RunService"] = true, ["RuntimeScriptService"] = true, ["ScreenGui"] = true, ["Script"] = true, ["ScriptContext"] = true, ["ScriptDebugger"] = true, ["ScriptService"] = true, ["ScrollingFrame"] = true, ["Seat"] = true, ["Selection"] = true, ["SelectionBox"] = true, ["SelectionLasso"] = true, ["SelectionPartLasso"] = true, ["SelectionPointLasso"] = true, ["SelectionSphere"] = true, ["ServerReplicator"] = true, ["ServerScriptService"] = true, ["ServerStorage"] = true, ["ServiceProvider"] = true, ["SessionService"] = true, ["Shirt"] = true, ["ShirtGraphic"] = true, ["SkateboardController"] = true, ["SkateboardPlatform"] = true, ["Skin"] = true, ["Sky"] = true, ["SlidingBallConstraint"] = true, ["Smoke"] = true, ["Snap"] = true, ["SocialService"] = true, ["SolidModelContentProvider"] = true, ["Sound"] = true, ["SoundEffect"] = true, ["SoundGroup"] = true, ["SoundService"] = true, ["Sparkles"] = true, ["SpawnerService"] = true, ["SpawnLocation"] = true, ["SpecialMesh"] = true, ["SphereHandleAdornment"] = true, ["SpotLight"] = true, ["SpringConstraint"] = true, ["StandalonePluginScripts"] = true, ["StandardPages"] = true, ["StarterCharacterScripts"] = true, ["StarterGear"] = true, ["StarterGui"] = true, ["StarterPack"] = true, ["StarterPlayer"] = true, ["StarterPlayerScripts"] = true, ["Stats"] = true, ["StatsItem"] = true, ["Status"] = true, ["StopWatchReporter"] = true, ["StringValue"] = true, ["Studio"] = true, ["StudioData"] = true, ["StudioService"] = true, ["StudioTheme"] = true, ["SunRaysEffect"] = true, ["SurfaceAppearance"] = true, ["SurfaceGui"] = true, ["SurfaceLight"] = true, ["SurfaceSelection"] = true, ["TaskScheduler"] = true, ["Team"] = true, ["Teams"] = true, ["TeleportService"] = true, ["Terrain"] = true, ["TerrainRegion"] = true, ["TestService"] = true, ["TextBox"] = true, ["TextButton"] = true, ["TextFilterResult"] = true, ["TextLabel"] = true, ["TextService"] = true, ["Texture"] = true, ["ThirdPartyUserService"] = true, ["TimerService"] = true, ["Tool"] = true, ["Torque"] = true, ["TotalCountTimeIntervalItem"] = true, ["TouchInputService"] = true, ["TouchTransmitter"] = true, ["Trail"] = true, ["Translator"] = true, ["TremoloSoundEffect"] = true, ["TriangleMeshPart"] = true, ["TrussPart"] = true, ["Tween"] = true, ["TweenBase"] = true, ["TweenService"] = true, ["UGCValidationService"] = true, ["UIAspectRatioConstraint"] = true, ["UIBase"] = true, ["UIComponent"] = true, ["UIConstraint"] = true, ["UIGradient"] = true, ["UIGridLayout"] = true, ["UIGridStyleLayout"] = true, ["UIInlineLayout"] = true, ["UILayout"] = true, ["UIListLayout"] = true, ["UIPadding"] = true, ["UIPageLayout"] = true, ["UIScale"] = true, ["UISizeConstraint"] = true, ["UITableLayout"] = true, ["UITextSizeConstraint"] = true, ["UnionOperation"] = true, ["UserGameSettings"] = true, ["UserInputService"] = true, ["UserSettings"] = true, ["UserStorageService"] = true, ["ValueBase"] = true, ["Vector3Value"] = true, ["VectorForce"] = true, ["VehicleController"] = true, ["VehicleSeat"] = true, ["VelocityMotor"] = true, ["VersionControlService"] = true, ["VideoFrame"] = true, ["ViewportFrame"] = true, ["VirtualInputManager"] = true, ["VirtualUser"] = true, ["Visit"] = true, ["VRService"] = true, ["WedgePart"] = true, ["Weld"] = true, ["WeldConstraint"] = true, ["Workspace"] = true, ["WorldModel"] = true, ["WorldRoot"] = true}, { __index = function() return false end });
print(classes['basePart']) --> true
print(classes["McDonalds"]) --> false
1 Like

@rek_kie try something like

local name = "InvalidPart"

if not pcall(function() Instance.new(name):Destroy() end) then  -- I called Destroy so that it doesn't take up memory probably
   print("invalid class") 
   return 
end
-- do it with name = "Part" then it'll not print "Invalid class"
-- a protected call will return true if the operation executed successfully

Edit: @Chatowillwin’s method won’t work as It won’t error if it’s not a valid class.

5 Likes

ok, I thought findfirstchildofclass might error if it is provided with something that isn’t a class.

1 Like

That is a lot of classes. I was thinking to do something like that too, putting all of the classes into a table. But that would take so much time :frowning:

This wouldn’t work very well because you’re depending on the workspace for the classes available

I’ll mark this as the solution for now as it works, even though it’s tedious. Thanks!