Help optimizing SMB3-Inspired GUI code

Hi guys! I’m pretty new to coding and have achieved a sort of level selection gui, inspired by the Super Mario Bros. 3 World Map design and sprite movement. Even though the movement from level-to-level works, I wonder if my code for it can be optimized (like lessening lines of code if possible).

Here’s a a script where my sprite moves from level 1 to 4.

	print("1to4 triggered")
	script.Parent.Lvl1to2.Disabled = true -- Every script except level 1 and 4 are disabled
	script.Parent.Lvl1to3.Disabled = true
	script.Parent.Level2.Lvl2to1.Disabled = true
	script.Parent.Level2.Lvl2to3.Disabled = true
	script.Parent.Level2.Lvl2to4.Disabled = true
	script.Parent.Level3.Lvl3to1.Disabled = true
	script.Parent.Level3.Lvl3to2.Disabled = true
	script.Parent.Level3.Lvl3to4.Disabled = true
	script.Parent.Level4.Lvl4to1.Enabled = true -- All scripts from level 4 are enabled
	script.Parent.Level4.Lvl4to2.Enabled = true
	script.Parent.Level4.Lvl4to3.Enabled = true 
	script.Parent.Play1.Visible = false
	script.Parent.Play4.Visible = true

I got a sample and video of the level select below if more examples are needed.

level select.rbxl (64.2 KB)

Some feedback would be, condense the line of code for disabling the level scripts. You can achieve this by using a for loop. for loops essentially loop through a folder or table and return its contents via a variable, most commonly the variable are named i and v, i stands for index and x is normally the child it’s up when looping through a folder. here is a video explain it too:

Also I curious what your game is about?

Thank you for the feedback! I’m planning to make a 2d platformer (inspired by mario) that includes level selecting, so I wanted to do that in a fun way!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.