GUI Not Disappearing?

So I’ve got this medkit that has a GUI instruction frame that shows up if you equip the tool and disappears if you unequip it. But for some reason, after you use the medkit and the tool disappears from the inventory, the GUI stays. I’ve tried using basic code like script.Parent.Instruct.Frame.BackgroundTransparency = 0, but that doesn’t seem to work. Help?

Medkit script:

local Tool = script.Parent;
enabled = true


function onActivated()
	if not enabled  then
		return
	end
	enabled = false
	Tool.Handle.Sound:Play()
	wait(1.5)
	Tool.Handle.Transparency = 1
	

	
	local h = Tool.Parent:FindFirstChild("Humanoid")
	if (h ~= nil) then
		wait(1)
		h.Health = h.Health + 100
	
	end
	
	wait(2.5)
	
	
	script.Parent:Remove()
	
end




function onEquipped()
	Tool.Handle.Equip:Play()
end

script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)

GUI script:

local gui

local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	if not game.Players.LocalPlayer.PlayerGui:FindFirstChild("Instruct") then -- Your Gui's name.
		gui = script.Parent.Instruct:Clone() -- Replace "TestGUI" with your Gui's name.
		gui.Parent = game.Players.LocalPlayer.PlayerGui
	end
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
	gui:Destroy()
end)

As far as I understood, you’re trying to hide(not destroy) the gui when you unequip the tool.
Am I correct?

yes. i only want to destroy the GUI when the tool is used up

Where’s the GUI script located?
In the tool itself, or in other location?

its in the tool itself, but won’t disappear wehn the tool itself is gone

Do you get any errors?
(character limit)

nope, no errors at all. (character limit)

local gui -- this is the problem

local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	if not game.Players.LocalPlayer.PlayerGui:FindFirstChild("Instruct") then 
		gui = script.Parent.Instruct:Clone()
		gui.Parent = game.Players.LocalPlayer.PlayerGui
	end
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
	gui:Destroy()
end)

Take a look at line 1, you’re destroying a variable when you unequip the tool;
You declared gui in the first statement, but you forgot to declare it in the second one.
Try setting it to gui = script.Parent:FindFirstChild("Instruct")

gui = script.Parent:FindFirstChild("Instruct")

local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	if not game.Players.LocalPlayer.PlayerGui:FindFirstChild("Instruct") then -- Your Gui's name.
		gui = script.Parent.Instruct:Clone() -- Replace "TestGUI" with your Gui's name.
		gui.Parent = game.Players.LocalPlayer.PlayerGui
	end
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
	gui:Destroy()
end)

it still doesn’t work

Try this:

local gui

local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	if not game.Players.LocalPlayer.PlayerGui:FindFirstChild("Instruct") then -- Your Gui's name.
		gui = script.Parent.Instruct:Clone() -- Replace "TestGUI" with your Gui's name.
		gui.Parent = game.Players.LocalPlayer.PlayerGui
	end
end)
script.Parent.Unequipped:Connect(function()
	gui = game.Players.LocalPlayer.PlayerGui:FindFirstChild("Instruct")
	equipped = false
	gui:Destroy()
end)

Can’t you just set the Enabled property of the Gui to false instead of cloning/deleting it every time it gets equipped?

gui = script.Parent:FindFirstChild("Instruct")

local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	if gui.Enabled = false then -- Your Gui's name.
		gui.Enabled = true
	end
end)

script.Parent.Unequipped:Connect(function()
	equipped = false
	gui.Enabled = false
end)
1 Like

still doesn’t work. the gui still stays after the tool was been destroyed


local Tool = script.Parent;

enabled = true

function onActivated()

    local gui=game.Players.LocalPlayer.PlayerGui:FindFirstChild("Instruct")

    enabled=not enabled

    if enabled==true then

    local h = Tool.Parent:FindFirstChild("Humanoid")

    Tool.Handle.Sound:Play()

    wait(1.5)

    Tool.Handle.Transparency = 1

    if (h ~= nil) then

        wait(1)

        h.Health = h.Health + 100       

    end

    wait(2.5)

    script.Parent:Destroy()

    gui:Destroy()

    else

    end

end

function onEquipped()

    Tool.Handle.Equip:Play()

end

script.Parent.Activated:connect(onActivated)

script.Parent.Equipped:connect(onEquipped)


local gui

local equipped = true

script.Parent.Equipped:Connect(function()

    equipped = not equipped

    if equipped==false then

        if not game.Players.LocalPlayer.PlayerGui:FindFirstChild("Instruct") then -- Your Gui's name. -- Replace "TestGUI" with your Gui's name.

            gui = script.Parent.Instruct:Clone()

            gui.Parent = game.Players.LocalPlayer.PlayerGui

        end

    else

        gui:Destroy()

    end

end)

script.Parent.Unequipped:Connect(function()

    equipped = false

end)

try and tell me if it worked

i tried the first and second one. none of them worked.

can you show the full hierarchy with the script

nope it didn’t work at all. i tried both of them

Boop.mp3

I feel like you didn’t even try this oof

it’s filled with errors, and it didn’t work when i tried it

Okay what are the errors then?

local Tool = script.Parent
player=game.Players.LocalPlayer
enabled = true
equipped = false
instruct = script.Parent.Instruct

function onActivated()
	local humanoid = Tool.Parent:FindFirstChild("Humanoid")
    enabled=not enabled
	if not enabled  then
		return
	end
	Tool.Handle.Sound:Play()
	wait(1.5)
	Tool.Handle.Transparency = 1
	if humanoid ~= nil then
		wait(1)
		humanoid.Health += 100
	end
	wait(2.5)
	Tool:Destroy()
end
script.Parent.Equipped:Connect(function()
	equipped = true
	if player:FindFirstChild("Instruct")~=nil then
        equipped=true
	    Tool.Handle.Equip:Play()
        instruct:Clone()
		instruct.Parent = player.PlayerGui
	end
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
    if player.PlayerGui:FindFirstChild("Instruct")~=nil then
        player:FindFirstChild("Instruct"):Destroy()
    end
end)
script.Parent.Activated:connect(onActivated)

try this