Want to make a settings menu, whats the best way?

Hello, recently I created a Options menu for my game, here is how I did it.

Main Module

--[[
functions:

local options = OptionsModule.CreateOptions(table, parent)
table -> the table with all the options you want to add
parent -> not necesary but where the options go after created

options:GetOptionValue(optionName) -- returns the value
options:SetOptionValue(optionName) -- set the value
optionName -> the name of the option, example "Volume"

]]

local OptionsModule = {}
OptionsModule.__index = OptionsModule

function OptionsModule.CreateOptions(optionsList, parent : Folder?)
	local self = setmetatable({}, OptionsModule)

	self.OptionsCreated = {}
	self.OptionsByName = {}

	local folder = Instance.new("Folder")
	folder.Name = "OptionsFolder"
	folder.Parent = parent or script

	for _, option in ipairs(optionsList) do
		local success, newOption = pcall(function()
			local instance = Instance.new(option.Type)
			instance.Name = option.Name
			instance.Value = option.StartedValue
			instance.Parent = folder
			return instance
		end)

		if success and newOption then
			table.insert(self.OptionsCreated, newOption)
			self.OptionsByName[newOption.Name] = newOption
		else
			warn("Error creating option:", option.Name)
		end
	end

	self.Folder = folder
	return self
end

function OptionsModule:GetOptionValue(optionName)
	local option = self.OptionsByName[optionName]
	if option then
		return option.Value
	else
		warn("Option not found:", optionName)
	end
end

function OptionsModule:SetOptionValue(optionName, newValue)
	local option = self.OptionsByName[optionName]
	if option then
		option.Value = newValue
	else
		warn("Option not found:", optionName)
	end
end

return OptionsModule

Use Example

local OptionsModule = require(script.Options) -- path to the module

local optionsList = { -- put any option you want to create
	{ Type = "BoolValue", Name = "MusicEnabled", StartedValue = true },
	{ Type = "IntValue", Name = "Volume", StartedValue = 5 },
}

local options = OptionsModule.CreateOptions(optionsList, game.ReplicatedStorage) -- creates the options

print(options:GetOptionValue("MusicEnabled")) -- true
options:SetOptionValue("Volume", 10)

Use the module on the client side obviously, create everything you want and then check if the player is activating the option, you can use sliders too, I used it with this module, really cool use.

1 Like