I assume this is happening because you're not setting the same GUI element's visibility in both scripts.
clickpart script:
1 | function onClicked(player) |
2 | print ( "Clickdetector was clicked!" ) |
3 | player.PlayerGui.ScreenGui.Frame.Visible = true |
5 | script.Parent.ClickDetector.MouseClick:Connect(onClicked) |
XButton script:
1 | script.Parent.MouseButton 1 Down:Connect( function () |
2 | script.Parent.Parent.Parent.Frame.Visible = false |
When you click the first time, the player.PlayerGui.ScreenGui.Frame
element is set as visible, so the code entry GUI becomes visible. When you click the X, you want to refer to the exact same element, so that one event makes it visible and the other invisible.
What I assume is happening is that the second (XButton) script is making the parent component of the code entry GUI invisible once it's clicked. Since the element the first script refers to (player.PlayerGui.ScreenGui.Frame
) is a child of the element being set to invisible, it is hidden regardless of whether its visibility is set to true or false. This is why you can only get the GUI to show once.
The solution to this is to make sure that script.Parent.Parent.Parent.Frame
and player.PlayerGui.ScreenGui.Frame
end up pointing at exactly the same element. You can look at the status of all GUI elements while testing the game in roblox studio to see what happens when each script is run, and which are visible/invisible in real-time.
Also, for future reference, you can make your code nicer to look at by putting it in a code block, which you can make by clicking the blue Lua button at the top of the text editor