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

01local guis = game.ReplicatedStorage.GUIs
02local player = script.Parent.Parent
03local phoneactive = script.Parent:FindFirstChild("Phone")
04 
05function 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
17end
18 
19game: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 — 9y

2 answers

Log in to vote
3
Answered by 9 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:

01local guis = game.ReplicatedStorage.GUIs
02local player = script.Parent.Parent
03 
04function 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
17end
18 
19game:GetService("UserInputService").InputBegan:connect(onKeyPress)
0
I never knew this. Thanks for sharing this mistake, ConnorVIII 448 — 9y
Ad
Log in to vote
0
Answered by 9 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.

01local GUI = script.Parent
02local Frame = GUI:WaitForChild("Frame")
03local Buttons = Frame:GetChildren()
04local Player = game.Players.LocalPlayer
05local Mouse = Player:GetMouse()
06 
07 
08 
09 
10Mouse.KeyDown:connect(function(key)
11    if key == "l" then
12        Frame.Visible = not Frame.Visible
13    end
14end)

If you have any questions just come ask me.

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

Answer this question