Make GUI appear with ClickDetector

So, I’ve done a lot of research on this and can’t seem to get it. I want to make a ClickDetector that makes a GUI pop up for everyone. Why won’t this work? When I test it in studio, it doesn’t work. But then, if I click "Current: Client " then it shows me the GUI enabled.

local ClickDetector = script.Parent.ClickDetector

ClickDetector.MouseClick:Connect(function(Player)
	game.StarterGui.ScreenGui.Enabled = true
end)
1 Like

You need to index the Gui replicated to a player’s PlayerGui, not game.StarterGui.

ClickDetector.MouseClick:Connect(function(Player)
    local PGui = Player.PlayerGui
	PGui.ScreenGui.Enabled = true
end)

If you need it to show for every client and you’re doing this through a server-script, do this

ClickDetector.MouseClick:Connect(function()
    for _, p in ipairs(game.Players:GetPlayers()) do
       local PGui = p.PlayerGui
       PGui.ScreenGui.Enabled = true
    end
end)
2 Likes

ClickDetectors only work with Scripts. If you want to display something on the client, you will need to use RemoteEvents with :FireAllClients(). If the gui was added to the player on the server, you can display it by accessing Player.PlayerGui.

2 Likes

If I click the button, I want the GUI to pop up for everyone.

2 Likes

That is incorrect,
you can use a LocalScript indexing the ClickDetector object and it’ll work along with its members (e.g the MouseClick event).

Who said LocalScripts only need to be in workspace???
LocalScripts should canonically (there’s not many places they work) be under StarterPlayerScripts, StarterCharacterScripts or a player’s PlayerGui .

1 Like

LocalScripts can’t execute in Workspace though?

image

Thank you! This worked! But one more question, since I don’t want to keep making topics. How would I make it so if you click it AGAIN, the gui disables?

1 Like

A prior edit from someone else said to put it under the ClickDetector which is what I was referring to. Referencing the ClickDetector would work.

1 Like

Just change this part:

    PGui.ScreenGui.Enabled = true
    #-- to
    PGui.ScreenGui.Enabled = not PGui.ScreenGui.Enabled 

so each time its clicked, the property toggles between false or true.

Would I have to make another function?

I would have checked if the MouseClick event with the click detector is fired on the server and when its fired you can send a remote event to the local script using the :FireAllClients function and yeah thats basically it.

It’s important to use the player’s gui which is the PlayerGui not the StarterGui, the StarterGui is there for only holding GUIS.

This is how I would do it:

Script

local Part = game.Workspace.Part

Part.ClickDetector.MouseClick:Connect(function()

game.ReplicatedStorage.RemoteEvent:FireAllClients() -- Fire the remote event

end)

Localscript

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function() -- picks up the remote event 

local ScreenGui = PlayerGui:WaitForChild("ScreenGui") -- creates a variable for the screengui (make sure you are using PlayerGui not StarterGui)

ScreenGui.Enabled = true

end)

The reason to why you shouldn’t be using the StarterGui is that its only there for holding GUIS I believe and you should be using PlayerGui because that is where all the gui is replicated to the player.

If you don’t know what :FireAllClients does, basically it makes sure that whatever you are doing is being viewed for every player.