How would I make this GUI not duplicate?

So, I want a gui to appear everytime a player clicks and object and the objects name will appear there, but… They can click an object multiple times and make the gui clone alot. I want it so every time it clicks, the existing gui dissapears and a new one pops up. Code:

local gui = script:WaitForChild("Inpect-Tool")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

script.Parent.Activated:Connect(function()
	local clone = gui:Clone()
	clone.Text.Text = mouse.Target.Name
	clone.Parent = player:WaitForChild("PlayerGui")
end)

It’s inside ‘Inspect’.

You can do this in many ways.

Personally, I would set the name of the gui to anything unique, I will choose “CloneUI”.
Then you can add a conditional statement to check if the Cloned UI already exists

local gui = script:WaitForChild("Inpect-Tool")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

script.Parent.Activated:Connect(function()
   if player:WaitForChild("PlayerGui"):FindFirstChild("CloneUI") then
       player:WaitForChild("PlayerGui"):FindFirstChild("CloneUI"):Destroy()
   end
	local clone = gui:Clone()
	clone.Text.Text = mouse.Target.Name
    clone.Name = "CloneUI"
	clone.Parent = player:WaitForChild("PlayerGui")
end)
2 Likes