Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Enable a ScreenGui with a ClickDetector in a Part?

Asked by 5 years ago
Edited 5 years ago

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)

0
Change Enabled to Visable. tawk1215 47 — 5y
0
Changed it, still nothing happened ReynaldoVictarion 70 — 5y
0
ScreenGui doesn't have a Visible property, Enabled was correct. However, click detectors are weird when dealing with client side stuff. chomboghai 2044 — 5y
0
I'll change it back then ReynaldoVictarion 70 — 5y
0
Reynaldo please check my answer Lolamtic 63 — 5y

4 answers

Log in to vote
1
Answered by 5 years ago

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!

0
I see the problem, thanks for your help. ReynaldoVictarion 70 — 5y
0
Hey, script works in studio but not in-game? The gui comes up, but the buttons don't work and are misplaced. ReynaldoVictarion 70 — 5y
0
Do you happen to know why this happens? ReynaldoVictarion 70 — 5y
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"? User#20279 0 — 5y
View all comments (3 more)
Ad
Log in to vote
1
Answered by 5 years ago

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!

0
This also works! Thanks! ReynaldoVictarion 70 — 5y
0
np! Lolamtic 63 — 5y
0
This one is simpler and easier to understand lol Lolamtic 63 — 5y
Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago
Edited 5 years ago

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!

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. chomboghai 2044 — 5y
0
"Enabled" doesnt work. "Visible" doesnt work. I edited my question ReynaldoVictarion 70 — 5y
0
Also what does it mean to be "Read-only"? I've never heard of that ReynaldoVictarion 70 — 5y
0
When something is read-only, this means you can access the property, however, you can't change it saSlol2436 716 — 5y
Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago

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!

0
Also doesn't work, re-edited my question. ReynaldoVictarion 70 — 5y

Answer this question