Visible false/true code

I want to make this script cleaner. I think I’m doing a lot of unnecessary things, but it works.


script.parent.MouseButton1Click:Connect(function()
	storymode.Visible = false
	Donate.Visible = false
	options.Visible = false
	FreePlay.Visible = false
	Background.Visible = false
end)
local storymode = script.parent.parent:WaitForChild("options")

script.parent.MouseButton1Click:Connect(function()
	storymode.Visible = false
	Donate.Visible = false
	options.Visible = false
	FreePlay.Visible = false
	Background.Visible = false
end)
local storymode = script.parent.parent:WaitForChild("Donate")

script.parent.MouseButton1Click:Connect(function()
	storymode.Visible = false
	Donate.Visible = false
	options.Visible = false
	FreePlay.Visible = false
	Background.Visible = false
end)
local options = script.parent.parent:WaitForChild("FreePlay")
script.parent.MouseButton1Click:Connect(function()
	options.Visible = false
	Donate.Visible = false
	storymode.Visible = false
	FreePlay.Visible = false
	Background.Visible = false
end)
local options = script.parent.parent:WaitForChild("Background")

script.parent.MouseButton1Click:Connect(function()
	options.Visible = false
	storymode.Visible = false
	FreePlay.Visible = false
	Donate.Visible = false
	Background.Visible = false
end)
local options = script.parent.parent:WaitForChild("Screen")

script.parent.MouseButton1Click:Connect(function()
	Screen.Visible = true
	options.Visible = false
	storymode.Visible = false
	FreePlay.Visible = false
	Donate.Visible = false
	Background.Visible = false
1 Like

I’m not too sure what your environment looks like since this is just code but I can make some assumptions based off of the way your code is structured and it looks like you have multiple LocalScripts, one per button to control visibility.

A better method you can rely on is to delete all those LocalScripts and instead make one LocalScript that’s in the location where the buttons are. When an option is clicked you can track which option was selected and then make its relevant frame visible. When a different option is clicked, first you make the last selected option invisible (if one is set) and then make the new one show.

A picture of your Gui’s hierarchy would help out in determining how this can be improved directly.

2 Likes

I will tell you a pro tip if you just make a frame and contained all of those stuff inside it would be easier lol thank me later :v:

1 Like

One way to clean it up is make an array of the items and use a single function to change visibility. Also agree with the other post to make a single local script not many.

local frames = {
script.parent:WaitForChild("Screen"),
script.parent:WaitForChild("options"),
...
}

function makeVisible(frameName)
  for _, frame in ipairs(frames) do
       frame.Visible = (frame.Name == frameName)
  end

end

script.Parent.options.someButton.MouseButton1Click:Connect(function()
  makeVisible("options")
end
4 Likes

thank you for all the answers, I will definitely test it!

A picture of your Gui’s hierarchy would help out in determining how this can be improved directly.

One way to clean it up is make an array of the items and use a single function to change visibility. Also agree with the other post to make a single local script not many.

Where can I insert something of my own into the script

1 Like