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

How do you show and hide gui when clicking a part?

Asked by 4 years ago

So I've been experimenting with trying to show gui when I click on an object. I found that disabling the gui and enabling the gui hides it and shows it. So this is the script I tried:

local ClickDetector = workspace.Volcano.Keypad.ClickDetector

function onMouseClick()
    game.StarterGui.Keypad.Enabled = true
end

ClickDetector.MouseClick:connect(onMouseClick)

This enables the gui but it doesn't show.

0
use playergui instead raid6n 2196 — 4y
0
how do you refer to playergui? OMGgaming89 2 — 4y
0
edited raid6n 2196 — 4y
0
^ raid6n 2196 — 4y

1 answer

Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You might want to use PlayerGui, so it only works for the player who clicked it.

To get the player, we can use a player added event.

Script:

game.Players.PlayerAdded:Connect(
    function(player)
        print(player.Name)
    end
)

Now, once we've done that, we'll have to detect if the player clicks the part.

game.Players.PlayerAdded:Connect(
    function(player)
        print(player.Name)
        local ClickDetector = workspace.Volcano.Keypad.ClickDetector

        function onMouseClick()
            print("Clicked)")
        end
        ClickDetector.MouseClick:Connect(onMouseClick)
    end
)

After that, we'll have to make it toggle enabled. To do this, we'll just have to do an if statement detecting if it's enabled or not.

game.Players.PlayerAdded:Connect(
    function(player)
        print(player.Name)
        local ClickDetector = workspace.Volcano.Keypad.ClickDetector

        function onMouseClick()
            print("Clicked)")
            if player.PlayerGui.Keypad.Enabled == false then
                player.PlayerGui.Keypad.Enabled = true
            else
                player.PlayerGui.Keypad.Enabled = false
            end
        end
        ClickDetector.MouseClick:Connect(onMouseClick)
    end
)

Accept the answer if it worked.

0
When I hit play, it says this error in the output. OMGgaming89 2 — 4y
0
12:46:50.704 - Workspace.Volcano.Keypad.Script:16: attempt to index nil with 'MouseClick' OMGgaming89 2 — 4y
0
It has the same result :/ OMGgaming89 2 — 4y
0
Thanks. It worked. OMGgaming89 2 — 4y
Ad

Answer this question