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

Cloned GUI wont go away?

Asked by 8 years ago

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 :)

0
maybe if you make a local variable called help = nil at line 4 then at line 9 help:Destroy() and at line 11 instead of local help make it say help. Hero_ic 502 — 8y

2 answers

Log in to vote
3
Answered by 8 years ago

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)
0
I never knew this. Thanks for sharing this mistake, ConnorVIII 448 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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.

0
KeyDown is depreciated and should never ever be used now. Hero_ic 502 — 8y

Answer this question