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:
local guis = game.ReplicatedStorage.GUIs local player = script.Parent.Parent local phoneactive = script.Parent:FindFirstChild("Phone") function onKeyPress(inputObject, gameProccessedEvent) if not gameProccessedEvent then -- If not using an event, run this code if inputObject.KeyCode == Enum.KeyCode.G then if phoneactive then script.Parent.Phone:Destroy() else local help = guis.Phone:Clone() help.Parent = player.PlayerGui player.PlayerGui.PlayerAudio.Pullup:Play() end end end end 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:
local guis = game.ReplicatedStorage.GUIs local player = script.Parent.Parent function onKeyPress(inputObject, gameProccessedEvent) if not gameProccessedEvent then -- If not using an event, run this code if inputObject.KeyCode == Enum.KeyCode.G then local phoneactive = script.Parent:FindFirstChild("Phone") if phoneactive then script.Parent.Phone:Destroy() else local help = guis.Phone:Clone() help.Parent = player.PlayerGui player.PlayerGui.PlayerAudio.Pullup:Play() end end end end 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.
local GUI = script.Parent local Frame = GUI:WaitForChild("Frame") local Buttons = Frame:GetChildren() local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() Mouse.KeyDown:connect(function(key) if key == "l" then Frame.Visible = not Frame.Visible end end)
If you have any questions just come ask me.