Gui wont become visible on click

I was going to make it where when you click a part an image label will become visible on a players screen. I’m new to scripting and am not sure why this doesn’t work.

I have a part with a click detector and inside that detector is a script:

local gui = game.StarterGui.ScreenGui.ImageLabel

function onClicked()
    gui.Visible = true
end

game.workspace.Part.ClickDetector.MouseClick:connect(onClicked)

Then all I have is an image label in StarterGui. I’m assuming you are going to have to use remote functions but I am not sure how I’m supposed to do that. Thanks

Simply because you’re using starterGui, when the player is in-game, starterGui becomes PlayerGui.

if you want to use a remote , you can do :


--Client
local gui = game.Players.LocalPlayer.PlayerGui.ScreenGui.ImageLabel
local remote = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteEvent")

function onClicked()
	remote:FireServer()
end

game.workspace.Part.ClickDetector.MouseClick:connect(onClicked)



--Server
local remote = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteEvent")

remote.OnServerEvent:Connect(function(Player)
	--open UI here
end)
1 Like

Instead of a separate function try this:

local gui = game.StarterGui:WaitForChild("ScreenGui"):WaitForChild("ImageLabel")

game.Workspace:WaitForChild("Part"):WaitForChild("ClickDetector").MouseClick:Connect(function()
    gui.Visible = true
end
1 Like

To help visualize what @Valkyrop said:

You need to define the player in a way of your choosing then

local gui = player.PlayerGui:FindFirstChild('ScreenGui'):FindFirstChild('ImageLabel')

function onClicked()
    gui.Visible = true
end

game.workspace.Part.ClickDetector.MouseClick:connect(onClicked)
1 Like

You’re right about needing to use a remote event. The onClicked() function should fire a remote event that causes the local script to display the image. The local script inside the GUI should have an onServerEvent function that just makes the image visible.

You can’t change a player’s GUI from the server, (not since the early days of Roblox,) because of Roblox’s server/client model. That’s why you’ll need to utilize a remote event.

1 Like

As previously stated, StarterGui changes will not effect the player. You can think of StarterGui as a container which holds stuff. Once a new client connects to an instance, StarterGui is cloned into PlayerGui. So, how do we access it with respect to your script?

Because you’re using a ClickDetector on MouseClick, you can make use of the automatically parsed parameter of MouseClick, which is the player who clicked. Try the following:


local function onClicked(plr)
   local gui = plr.PlayerGui.ScreenGui.ImageLabel
   gui.Visible = true 
end

workspace.Part.ClickDetector.MouseClick:Connect(onClicked)

As shown here, it is possible without RemoteEvents, however I personally recommend actually using RemoteEvents as I prefer to keep all UI interactions on the client. If you’d like a sample script to go off of using RemoteEvents instead, let me know.

3 Likes

Thank you, this works very well. It would be better using a remote event, that would be much appreciated.

Under the assumption you’d like to use a RemoteEvent, here is an example script(s). This requires a RemoteEvent in ReplicatedStorage. You could do it this way, or simply detect the MouseClick on the client as well.

-- server
local function onClicked(plr)
   game.ReplicatedStorage.RemoteEvent:FireClient(plr)
end

workspace.Part.ClickDetector.MouseClick:Connect(onClicked)
--client

local function VisibilityChange()
   local gui = PathToTheGuiGoesHere --this depends on where the script is, for example script.Parent
   gui.Visible = true 
end

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(VisibilityChange)
1 Like

Thank you very much. So I assume I put these lines of codes in two different scripts somewhere? I’m very new too this sorry.

No worries. The client sided script would be a LocalScript somewhere in StarterGui. The serversided script you can probably leave to where it was before.

1 Like

Thanks so much, it worked fine. And thank you everyone else as well. Have a good day.