Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Enable a ScreenGui with a ClickDetector in a Part?

I want to enable a ScreenGui I have in StarterGui by clicking on a part.

No errors in output, nothing happens when I click on the part.

Can someone please tell me what I am doing wrong?

-- LocalScript inside StarterPlayerScripts
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local gui = game.StarterGui.WeaponsGui -- path to WeaponsGui

mouse.Button1Down:Connect(function()
    if mouse.Target and mouse.Target.Name == "GuiGiver" then
        local clickDetector = mouse.Target:FindFirstChild("ClickDetector")
        if clickDetector then
            gui.Enabled = true
        end
    end
end)

5 comments

0

Change Enabled to Visable.

0

Changed it, still nothing happened

0

ScreenGui doesn't have a Visible property, Enabled was correct. However, click detectors are weird when dealing with client side stuff.

0

I'll change it back then

0

Reynaldo please check my answer

4 answers

✓ Accepted answer

I couldn’t test my code so sorry if anything is wrong.

So there’s one mistake that is REALLY sticking out to me (and surprisingly no one noticed it yet) and that is line 5 on your code. You’re trying to enable your GUI through StarterGui, which won’t work as StarterGui clones all GUI in it into players that joined/respawned. What you’re trying to get to is PlayerGui, which is in the player.

Here’s what your code should look like :

-- LocalScript inside StarterPlayerScripts
local player = game:GetService('Players').LocalPlayer
local mouse = player:GetMouse()

local gui = player:WaitForChild('PlayerGui').WeaponsGui -- path to WeaponsGui

mouse.Button1Down:Connect(function()
    if mouse.Target and mouse.Target.Name == "GuiGiver" then
        local clickDetector = mouse.Target:FindFirstChild("ClickDetector")
        if clickDetector then
            gui.Enabled = true
        end
    end
end)

Now, since I can’t test this, I can’t guarantee the code works, but I’m sure that you have to enable it through PlayerGui.

Also note that if you switch back to using the MouseClick event on the clickdetector, it may or may not work. I forgot if the event works locally but if it doesn’t work, you’ll have to use remote events.

Hope this helped!

7 comments

0

I see the problem, thanks for your help.

0

Hey, script works in studio but not in-game? The gui comes up, but the buttons don't work and are misplaced.

0

Do you happen to know why this happens?

0

Tested the code and it works for me. You did do what the comment on the first line said right? Also the part has to be named "GuiGiver" and there must a ClickDetector in it.  Also can you explain more on the buttons "not working and being misplaced"?

0

Thats how the gui looks in-game

Please put a ClickDetector in the part and instert this script!

script.Parent.MouseClick:Connect(function(plr)
	plr.PlayerGui.WeaponsGui.Enabled = true
end)

This short simple code will make it work fine!

3 comments

0

This also works! Thanks!

0

np!

0

This one is simpler and easier to understand lol

Hello! This is question actually has a simple answer. See, the enabled part of your script is the reason. Enabled is read-only, so it can't be edited.

Instead, use Visible for the text label/frame in order to do this. Thanks!

4 comments

0

What? Enabled is not read-only. It can definitely be edited. Please make sure you are providing correct answers to the users who need help.

0

"Enabled" doesnt work. "Visible" doesnt work. I edited my question

0

Also what does it mean to be "Read-only"? I've never heard of that

0

When something is read-only, this means you can access the property, however, you can't change it

The problem is mostly with the ClickDetector object. It's annoying to get around. If your script is a local script inside the click detector, that won't work cause localscripts only run under player objects. One solution would be to check on the client side for this click detector.

-- LocalScript inside StarterPlayerScripts
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local gui = -- path to WeaponsGui

mouse.Button1Down:Connect(function()
	if mouse.Target and mouse.Target.Name == "NameOfObjectHoldingClickDetector" then
		local clickDetector = mouse.Target:FindFirstChild("ClickDetector")
		if clickDetector then
			gui.Enabled = true
		end
	end
end)
			

Hope this helps!

1 comment

0

Also doesn't work, re-edited my question.

This discussion is preserved as it was. New answers, comments, and votes are closed.