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

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

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.

4 comments

0

use playergui instead

0

how do you refer to playergui?

0

edited

0

^

1 answer

✓ Accepted answer

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.

4 comments

0

When I hit play, it says this error in the output.

0

12:46:50.704 - Workspace.Volcano.Keypad.Script:16: attempt to index nil with 'MouseClick'

0

It has the same result :/

0

Thanks. It worked.

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