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

Cloned GUI wont go away?

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

1 comment

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.

2 answers

✓ Accepted answer

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)

1 comment

0

I never knew this. Thanks for sharing this mistake,

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.

1 comment

0

KeyDown is depreciated and should never ever be used now.

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