How can I make this toggle more easy to write without spamming variables

This toggle system has been very time consuming for me to write and when you have several functions that need to use something like this, you will end up with 10 variables that are just “order2, order3, order4”

any help on writing this a little better?

local order = 1
gui.Buttons.Menu.MouseButton1Click:Connect(function()
	if order == 1 then
		order = 2
	elseif order == 2 then
		order = 1
	end
end)

edit: Turns out I was accidentally calling this a debounce lol.

2 Likes

What IS this.

If you really need this and other debounce methods do not work for you then sure, but how.

It’s way easier to just do

local Deb = false

local function DoSomething()
	if Deb then return end
	Deb =  true
	-- // Do something
	task.wait(1)
	Deb = false
end
1 Like

Cool script. Not sure if this belongs under my post

3 Likes

I think you can just use a bool instead.

local enabled = false
gui.Buttons.Menu.MouseButton1Click:Connect(function()
enabled = not enabled 
	if enabled then
print("enabled")
	else
print("disabled")
	end
end)

Assuming its going from 0, 1, 2, 3, (or starting from any number for that matter going up/down)

You can use a for loop:

Could you show your script if you were to just do it this way (so that we could think of a way to do it better)

Here’s one example

local toggle = false
local toggle2 = false

--{{ Menu Buttons }}--

local function menuClick()
	if toggle == false then
		util.tweenPos(frame, UDim2.new(-0.5, 0,0.244, 0), .4)
		util.tweenPos(frame2, UDim2.new(0.002, 0,0.571, 0), .4)
		task.wait(.5)
		toggle = true
	elseif toggle == true then
		util.tweenPos(frame, UDim2.new(0.023, 0,0.244, 0), .4)
		util.tweenPos(frame2, UDim2.new(0, 0,0.5, 0), .4)
		task.wait(.5)
		toggle = false
	end
end

button.MouseButton1Click:Connect(menuClick)

local function standClick()
	if toggle2 == false then
		util.tweenPos(frame_stand, UDim2.new(0.001, 0,0.025, 0), .4)
		task.wait(.5)
		toggle2 = true
	elseif toggle2 == true then
		util.tweenPos(frame_stand, UDim2.new(0.001, 0,-2, 0), .4)
		task.wait(.5)
		toggle2 = false
	end
end

1 Like

Numbers and Bools aren’t the issue for me, it’s the variable and if statement spam that bothers me

You can actually use a conditional operator to makes things shorter.

local function menuClick()
	util.tweenPos(frame, if toggle then UDim2.new(0.023, 0,0.244, 0) else UDim2.new(-0.5, 0,0.244, 0), .4)
	util.tweenPos(frame2, if toggle then UDim2.new(0, 0,0.5, 0) else UDim2.new(0.002, 0,0.571, 0), .4)
	task.wait(.5)
	toggle = not toggle
end

Here’s an example:

local bool = true

warn(if bool then "its true" else "its false") -- either one will be printed based on the bool

If the boolean is true, it will print out “its true”, but if it’s false, it will print out “its false”. This looks way more cleaner than:

local bool = true

if bool then
	warn("its true")
else
	warn("its false")
end
1 Like

everyone is recommending booleans when you can just make it in 1 line, here it switches the order from 1 to 2 and opposite, does what your code does, in a better way.

local order = 1
gui.Buttons.Menu.MouseButton1Click:Connect(function()
	order = order ~= 1 and 1 or 2
end)
1 Like

I figured something similar out, what I don’t understand is how I can write something inside it,

Is it like:

order = order == 1 and 2 or 1 
if order == 1 then function() else function2()
1 Like

I see you want to play a function, lemme write a quick code for you.

here it is, just the same idea.

function func1()
	
end

function func2()
	
end

local order = 1

gui.Buttons.Menu.MouseButton1Click:Connect(function()
	order = order ~= 1 and 1 or 2
	
	local functionToPlay = order == 1 and func1 or func2
	functionToPlay()
end)
4 Likes

You can’t really do that whenever you are not assigning things to variables. So instead, you just add an “end” keyword.

if order == 1 then function() else function2() end
2 Likes