This tutorial will guide you through creating and using custom Enums in Roblox using Lua scripting. Custom Enums allow for better organization of your code and can make your projects more modular and easier to manage.
Example
local Enum = require(path.to.Enum)
-- Define the type for ExampleEnum using TypeScript-style type definition (used for reference in Lua)
-- The type specifies that 'Default' and 'Something' are EnumItems belonging to an Enum
export type ExampleEnum = {
Default: EnumItem,
Something: EnumItem
} & Enum & Enum.Extension
-- Create and return the ExampleEnum Enum with 'Default' and 'Something' as Enum items
return Enum('ExampleEnum', {'Default', 'Something'}) :: ExampleEnum
Usage
Plugin
This plugin automatically includes both default Roblox Enums and your custom Enums, allowing for seamless integration and management of Enum types in your game.
New Update now you can put Enums files to anywhere
Script setup for Enums not (Enum)
local Enum = require(path.to.Enums)
Enum() <-- This will set the environment with the custom typeof function
print(typeof(Enum.CustomEnum), typeof(Enum.CustomEnum.Default))
--Return string("Enum", "EnumItem") default string is "table"
--Roblox don't have feature to allow users to custom environment so this mean you should require Enums to every where you want to get typestring from your custom enums
Benefits:
- Organize your game by defining custom Enums for various purposes.
- Prevent errors by ensuring values are restricted to predefined Enum items.
- Easily compare Enum items and check their types for more control.
Feel free to use and modify this script in your Roblox projects! Let me know if you have any questions or suggestions.
Note: Although this module is just a small feature that anyone can create on their own, I just wanted to contribute to the forum and share what I’ve made for others to try out. There’s no need to read or pay attention if it’s not something you’re interested in.
Resource Link Enum