Help with replicating a gui

So the issue is that the screen gui keeps on cloning when you touch it. How do I make it so when you touch it, it only clones once each time?

https://gyazo.com/7bd97af2091fc879423daa477e02f11f

So basically as you see it clones multiple times. I basically want it so that it does not do that and only replicates it once.

local Prox = script.Parent
local GUI = script.Parent:FindFirstChildOfClass("ScreenGui")

Prox.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Clone = GUI:Clone()
	Clone.Parent = Player.PlayerGui
	Prox.TouchEnded:Connect(function(hit2)
		if hit == hit2 then
			game.Debris:AddItem(Clone, 0)
		end
	end)
end)
1 Like

I’m not so sure but try removing th part where is says

Prox.TouchEnded:Connect(function(hit2)
		if hit == hit2 then
			game.Debris:AddItem(Clone, 0)
		end
	end)

that part destroys the gui if you step off of it…Not sure if that will fix the issue.

Ohhhh wait I misunderstood you for just wanting the gui to stay once… lol

Try doing something like

local Prox = script.Parent
local GUI = script.Parent:FindFirstChildOfClass("ScreenGui")
local cloned = false

Prox.Touched:Connect(function(hit)
	local Player = game.Parent:GetPlayerFromCharacter(hit.Parent)
	local Clone = GUI:Clone()
	if cloned == false then
		Clone.Parent = Player.PlayerGui
		cloned = true
	end
	Prox.TouchedEnded:Connect(function(hit2)
		if hit == hit2 then
			game.Debris:AddItem(Clone, 0)
		end
	end)
end)
local Players = game:GetService("Players")

local Part = script.Parent
local Gui = Part:FindFirstChildOfClass("ScreenGui")

Part.Touched:Connect(function(Hit1)
	local HitModel = Hit1:FindFirstAncestorOfClass("Model")
	if HitModel then
		local HitPlayer = Players:GetPlayerFromCharacter(HitModel)
		if HitPlayer then
			local PlayerGui = HitPlayer.PlayerGui
			if PlayerGui:FindFirstChild(Gui.Name) then
				return
			end
			
			local GuiClone = Gui:Clone()
			GuiClone.Parent = HitPlayer.PlayerGui
			
			Part.TouchedEnded:Connect(function(Hit2)
				if Hit1 == Hit2 then
					GuiClone:Destroy()
				end
			end)
		end
	end
end)

There’s a good chance your script in its current state will error, also it’s better to check if the player’s “PlayerGui” folder contains the gui in question rather than using a state variable/flag to determine as such.

1 Like