Hi, so, I'm trying to make a game for my brother, and we have basically everything done. I click on the vault to enter the code, and if you type it correctly, or exit out of it, and then try to click it again, it doesn't do what it just did before. Can I get some help on this?
clickpart script:
function onClicked(player) print("Clickdetector was clicked!") player.PlayerGui.ScreenGui.Frame.Visible = true end script.Parent.ClickDetector.MouseClick:Connect(onClicked)
XButton script:
script.Parent.MouseButton1Click:Connect(function() script.Parent.Parent.Parent.Frame.Visible = false end)
(The correct code script is too long so I'm not gonna put it here.)
GIF Of this happening: https://gyazo.com/9d32aa8acff85bce83aa2ca224ffe2c8
I assume this is happening because you're not setting the same GUI element's visibility in both scripts.
clickpart script:
function onClicked(player) print("Clickdetector was clicked!") player.PlayerGui.ScreenGui.Frame.Visible = true end script.Parent.ClickDetector.MouseClick:Connect(onClicked)XButton script:
script.Parent.MouseButton1Down:Connect(function() script.Parent.Parent.Parent.Frame.Visible = false end)
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
I have simply solved this problem by using a Remote Event for my GUI. I have accepted whenallthepigsfly
's answer, because he was the only one to answer the question.