Best way of disabling part of a code

Hello !

I am working on a custom script control system for my game and I was wondering what was the best way of skipping part of a code when it is disabled.

A quick example would be to have a setting that would inverse the mouse motion when it is on and not when off.

The simplest solution I found was to use if statements but I still wonder if there would be a better way. Also note that the parts of the codes are stored in a table in separate function so keep that in mind.

Thanks!

I believe if statements are the simplest, and most effective way to go about this.

2 Likes

if statements are ideal or you can have a boolean value and then check when it changes to run different functions using .Changed or :GetPropertyChangedSignal(“Value”) if its an instance.

and if you want some sort of control over the functions that are being ran you can try coroutines

States are really helpful for this kind of thing. I use BasicState.

For example

local State = require(Somewhere.BasicState)

local Settings = State.new {
	Inverted = false
}

Settings:GetChangedSignal("Inverted"):Connect(function()
	if Settings:Get("Inverted") then
		
	else
		
	end
end)

Settings:Toggle("Inverted") -- true
Settings:Toggle("Inverted") -- false
Settings:Toggle("Inverted") -- true

unless its a REALLY specific case, im pretty certain the if statement is the best way to do it (or sometimes the while loop, if you might want that part of the code to be in a while loop ofc)

1 Like