You press G and this GUI comes up. I want it so when you press it again the GUI will disappear. Instead it just makes another one come up and you can virtually make an infinite amount of these GUI's appear. Any way to fix this here? Here's the code:
01 | local guis = game.ReplicatedStorage.GUIs |
02 | local player = script.Parent.Parent |
03 | local phoneactive = script.Parent:FindFirstChild( "Phone" ) |
04 |
05 | function onKeyPress(inputObject, gameProccessedEvent) |
06 | if not gameProccessedEvent then -- If not using an event, run this code |
07 | if inputObject.KeyCode = = Enum.KeyCode.G then |
08 | if phoneactive then |
09 | script.Parent.Phone:Destroy() |
10 | else |
11 | local help = guis.Phone:Clone() |
12 | help.Parent = player.PlayerGui |
13 | player.PlayerGui.PlayerAudio.Pullup:Play() |
14 | end |
15 | end |
16 | end |
17 | end |
18 |
19 | game:GetService( "UserInputService" ).InputBegan:connect(onKeyPress) |
Any help is appreciated :)
Ok, what you've done is a silly but common mistake. You have defined phoneactive
at the start of the script, but that value will never change once it's been defined even if later the GUI does exist. What you need to do is move then inside the key pressed event to make it check whether the phone is active when it's pressed. Your code should look like this:
01 | local guis = game.ReplicatedStorage.GUIs |
02 | local player = script.Parent.Parent |
03 |
04 | function onKeyPress(inputObject, gameProccessedEvent) |
05 | if not gameProccessedEvent then -- If not using an event, run this code |
06 | if inputObject.KeyCode = = Enum.KeyCode.G then |
07 | local phoneactive = script.Parent:FindFirstChild( "Phone" ) |
08 | if phoneactive then |
09 | script.Parent.Phone:Destroy() |
10 | else |
11 | local help = guis.Phone:Clone() |
12 | help.Parent = player.PlayerGui |
13 | player.PlayerGui.PlayerAudio.Pullup:Play() |
14 | end |
15 | end |
16 | end |
17 | end |
18 |
19 | game:GetService( "UserInputService" ).InputBegan:connect(onKeyPress) |
Alright brother. Here ya go. When you use this script, what you have to do is have a ScreenGui, within the screengui, have the frame you want to be opened and name that Frame and this script goes into a Local Script. The Local Script's parent needs to be the screengui. Then bam. It works.
01 | local GUI = script.Parent |
02 | local Frame = GUI:WaitForChild( "Frame" ) |
03 | local Buttons = Frame:GetChildren() |
04 | local Player = game.Players.LocalPlayer |
05 | local Mouse = Player:GetMouse() |
06 |
07 |
08 |
09 |
10 | Mouse.KeyDown:connect( function (key) |
11 | if key = = "l" then |
12 | Frame.Visible = not Frame.Visible |
13 | end |
14 | end ) |
If you have any questions just come ask me.