Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

GUI closing Script?

I'm making a GUI Intro for all my games. I've created the GUI, and I've done this:

print("Close button loaded")

button = script.Parent
window = script.Parent.Parent.Parent.Parent

function onClicked(GUI)
	window:remove()
end
script.Parent.MouseButton1Click:connect(onClicked)

But it isn't working. Help me please!

My GUI is -StarterGUI -IntroGUI -Frame,Textbox -TextBox -Script

7 answers

✓ Accepted answer
local Button = script.Parent -- TextButton
local Window = script.Parent.Parent.Parent.Parent -- ScreenGui or Frame, whatever is being removed.

Button.MouseButton1Click:connect(function()
	Window:Destroy()
end)

Remember to use a TextButton and make sure you have the directory correct. The use of an anonymous function allows you to do this much more efficiently with less typing work.

Try using the line window.Visible = false instead of removing it.

Also, what output are you getting?

1 comment

0

My output is nothing's happening.

So,

print("Close button loaded")

button = script.Parent
window = script.Parent.Parent.Parent.Parent

function onClicked(GUI)
    window.Visible = false
end
script.Parent.MouseButton1Click:connect(onClicked)

I'll try it. And the output is nothing happens.

Okay, I just tried it. Nothing Happen.

 print("Close button loaded")

button = script.Parent
window = script.Parent.Parent.Parent.Parent

function onClicked()
    window:Destroy()
end
script.Parent.MouseButton1Down:connect(onClicked)

Try that one, It might work.

It is because you have a TextBox. Use a TextButton instead.

1 comment

0

I have done that. Still nothing has happened.

I've encountered this problem many times and have tried and tried to fix it. Eventually I fixed it. You have to put the code into a local script and insert it into the GUI making it's parent the GUI, NOT the button. So it looks like this: StarterGUI - YourGUI - LocalScript . And as for the code:

local button = script.Parent.[FrameNameGoesHere]
local window = script.Parent.[FrameNameGoesHere]
local variable = script.Parent.[FrameNameGoesHere]


function onClicked(GUI)
	window:Destroy()
    
end
button.MouseButton1Click:connect(onClicked)

Note that I have removed print("Close button loaded"), you don't need that. It's sort of a line of unnecessary code users put into their scripts as some way of assuring it works. Well it makes no difference at all! It won't make your script run any differently, so I suggest you leave it out.

Note also that the local tags are added because they only apply to a certain player, not everyone in the game. You will not need to use the local tags later in the code when calling the variables.

Note finally that I have changed script.Parent.MouseButton1Click:connect(onClicked) to button.MouseButton1Click:connect(onClicked), this is because we changed the location of the script meaning script.Parentis no longer valid and will throw errors when tested. We can also use our variable 'button' in that line as it is the same as script.Parentbut with the correct layout.

This worked for me, and should work for you.

And also, if you are using a TextBox please change it to a TextButton as a TextBox is for typing/entering and a TextButton is for clicking.

3 comments

0

Try running your game in Solo with the Output Window. There will be no errors when you click and the GUI will close normally.

0

Apperently the first line isn't correct, as it is underlined in red.

0

Have you changed what was in my code to suit yours? Otherwise, of course it will be red

u cant make a Gui invisible... u can only do it to its children like Fram or a Button etc...

This discussion is preserved as it was. New answers, comments, and votes are closed.