How can I implement my own Enumerations?

Hello.

I am trying to create a system similar to Enumerations to enable the server and client to verify information. For example:

--server
local enum_database = require(enum_database_module)
function action(plr,enum_sent_by_client)
 if enum_sent_by_client == enum_database.crawl then
  ...
 end
end

game.ReplicatedStorage.RemoteEvent:Connect(action)
--client
local enum_database = require(enum_database_module)
game.ReplicatedStorage.RemoteEvent:Fire(enum_database.crawl)

I have tried to use tables and userdata but those are not consistent between the server and client, so they would not work for this situation. I am tempted to use numbers, but over time that may be more difficult to manage.

Does anyone know how to go about implementing something like this?

Thanks.

1 Like

An interesting read.

Create a module script in ReplicatedStorage that acts as a table for your custom enums then both the server and client can require it without the need of remote events.

-- custom enums example
return {
    ["YourEnum"] = 10,
}

-- script/local script
local enums = require(game.ReplicatedStorage.CustomEnumsModule)

print(enums.YourEnum) -- 10

Hey,
I know im late but i just wanted to mention another solution in case people dont want to use 3rd party modules.

If you use export type you can make a enum alike value aswell.

example:

export type PizzaSize = "15" | "20" | "25"
local t: PizzaSize = "20"

print(t)
8 Likes

Hello : ) sorry for kinda necroposting but I’ve been doing things like this and found it pretty useful:

local function setStatus(newStatus : "INTERMISSION" | "GAMESTART" | "GAMEPLAY" | "GAMEEND")
	RoundStatus.Value = newStatus
end

setStatus("INTERMISSION")

Not exactly enums but acts kinda like it, when you type " in the setStatus() function it brings up a dropdown list and you can just choose with arrow keys out of the possible you listed in the function paramaters. Works pretty well in modules like others mentioned making too.