Opens GUI when a block is clicked - code doesn't work

So, I am trying to make a menu that’s when clicked it opens up a GUI so the player can view it easier, however the code I made isn’t working (I am a builder and my Lua knowledge is very bad haha)
I did insert a ClickDetector into the part, and put the GUI of the menu in “StarterGui”.

The code is:

local button = game.Workspace:WaitForChild("FoodPage")
local clickDetector = button:WaitForChild("ClickDetectorOne")
local gui = script.Parent:WaitForChild("Food")

local function onClicked(player)
	gui.Visible = not gui.Visible
end

clickDetector.MouseClick:Connect(onClicked)

I tried watching some videos, but nothing helped.
It does hover over the menu, but when clicked it doesn’t open the GUI.

(The menu is a Model, with 2 pages inside of it - FoodPage and DrinkPage; both got click detectors: the foodpage is called ClickDetectorOne, and the drinkpage is ClickDetectorTwo)

1 Like

So first could you give me a little bit more detail

Oh wait so basically you have to set the GUI’s parent to player.PlayerGui

local button = game.Workspace:WaitForChild("FoodPage")
local clickDetector = button:WaitForChild("ClickDetectorOne")
local gui = script.Parent:WaitForChild("Food")

local function onClicked(player)
	gui.Visible = not gui.Visible
       gui.Parent = player.PlayerGui --sets the gui's parent to the client's player screen
end

clickDetector.MouseClick:Connect(onClicked)

tell me if this works!

Ok questions, what type of script are you using? (local script or script)
Where is your GUI parented?

This would be a bad idea to do, as from the look of it, my hunch is they arent using a local script, thus if someone were to activate the button, it would well parent it to them, but if someone else clicks the button, the first person will no longer have the UI, but the new player will have it.

That assumes they arent using the use of a local script, so in the cast that they are using a local script this would work.

I am using a regular script, and my GUI is located in StarterGUI as I mentioned. Should I add the line he added and change the script to a local script?

no, all you would need to do is just change the script to a local script and it should work, I dont use ClickDetctors much so it might not work, but only one way to find out!

1 Like

I changed it and nothing changed sadly haha. But thanks for the suggestion :grin:

1 Like

Ahh I see, can I see your current code? And also an image of your workspace? I think my brain is being silly so I gotta see your current progress :]

Here you go!
Current code: (note, both FoodPage and DrinkPage got different codes)

local button = game.Workspace:WaitForChild("FoodPage")
local clickDetector = button:WaitForChild("ClickDetectorOne")
local gui = script.Parent:WaitForChild("Food")

local function onClicked(player)

	gui.Visible = not gui.Visible
	gui.Parent = player.PlayerGui

end
clickDetector.MouseClick:Connect(onClicked) --added from the reply above, changed nothing

StarterGui:
StarterGui Food and Drink

Workspace:
Workspace Menu

I think I see the problem

FoodPage is under a model called Menu, so that might be it

Sadly it didn’t when I changed it to Menu, it’s alright lol. I’ll figure something out, thank you for your time though! <3

1 Like