How do I activate a GUI using a Click Detector?

So I’ve been developing a RP Game. I’ve been trying to link a Click Detector to activate a GUI. I want a Car Spawn GUI to appear when a block is clicked.
This is the Current script I have for activating it but it won’t work:

local click = script.Parent.Parent.Parent.Workspace.Spawn:WaitForChild("ClickDetector")
local frame = script.Parent:WaitForChild("CarSpawner")

click.MouseButton1Click:Connect(function()
frame.Visible = true
end)

Could a IFTHEN statement work?

16 Likes

Could you be more specific? More context?
Where are these scripts? Is it a server or local scripts?
Where is the GUI placed?
What actually doesn’t work?

Please do follow the guidelines and this thread does not belong here, it should be in #development-support:scripting-support

As I understand from your question, you are trying to activate a GUI when you click a Part.

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	player:WaitForChild("PlayerGui").ScreenGui.Frame.Visible = true
end)
--[[
 Replace ScreenGui with your GUI name.
--]]

Do not use a LocalScript for MouseClick event.

Also, please read About the Art Design Support category.
This thread should be in #development-support:scripting-support.

33 Likes

The Script is a Child to the GUI

1 Like

If you want a GUI to appear when you click a part then you’ll need to use a ClickDetector. This goes under your basepart. To interact with it in your case, you’ll want to use the .MouseClick event. Then you can do what you want to do.

Are you mistaken for something else?

1 Like

Thanks!! It worked! Have a Great day!!

1 Like

This method is fine when you are using a clickdetector as described above, but do not use this in other scripts!
It could yield whole server-scripts if the client does not have the PlayerGui for any reason, or deleted it.

I would recommend using a RemoteEvent that sends the signal to the player to display the UI.

3 Likes

I meant MouseClick instead of MouseButton1Click. My bad.

2 Likes

Also, I’ve been try to Tween the GUI but How do I make it tween Open?

1 Like

And how would I make the Script repeat so that I could close and open over and over again? Right now I can only Open it once and close it.

2 Likes

Here is an example of GUI Tweening:

--[[
	Open Position: {0.25, 0},{0.398, 0}
	Close Position: {0.25, 0},{-0.25, 0}
--]]

local Frame = script.Parent
local Open  = false
local Debounce = false

function ToggleTween()
	if Open == false and Debounce == false then
		Debounce = true
		Frame:TweenPosition(UDim2.new(0.25, 0,0.398, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Bounce,0.75,false)
		wait(1)
		Open = true
		Debounce = false
	elseif
		Open == true and Debounce == false then
			Debounce = true
			Frame:TweenPosition(UDim2.new(0.25, 0,-0.25, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Bounce, 0.75, false)
			wait(1)
			Open = false
			Debounce = false
	end
end

-- Now use ToggleTween() to toggle the GUI everytime you want.

while wait(3) do
	ToggleTween()
end

You can use any other method to toggle it with the ToggleTween() function. The while wait(3) do is to test it. Here is the GUI example:
GUI.rbxm (2.4 KB)

To insert the GUI:

  • Right click on StarterGUI
  • Insert from file
  • Find and select GUI.rbxm and click Open
7 Likes

Can I please remind that spoon feeding code isn’t what these categories are made for. Its intended to support other users into getting to the goal they intend to.

5 Likes

this doesnt work more than one time

1 Like