How do i make this ClickDetector script delete if theres a another GUI occupying the screen?

Hi, im making a tower defense game and i want it to like delete a GUI if it has one, and all towers has the name ‘‘UpgradeGUI’’ how do i make it so if you click on them it deletes if there is a GUI named ‘‘UpgradeGUI’’ so it doesn’t cause GUI overlaying over upgrades.

gui = script.Parent.UpgradeTower

function onClick(click)
	if click.Name == script.Parent.Parent.creator.Value then
		local guicopy = gui:Clone()
		guicopy.Parent = click.PlayerGui
	else
		workspace.error:Play()
	end
end
script.Parent.ClickDetector.MouseClick:connect(onClick)
1 Like

I didn’t understand completely but if you’re trying to delete a GUI then set Transparency to 1 (then you can get it back by setting Transparency to 0)

or you can use :Destroy to completely remove the GUI (and you cannot get it back)

i hope this helps

1 Like

i mean like, lets say you have 2 towers right? and you click the first one, and then if you click on the second tower, it deletes that UpgradeGUI from the first one, so it doesn’t overlay on screen.

1 Like

oh ill try to help.

so i think you can do:

local Detector = -- choose the clickdetector in the tower
local GUI1 = First upgradegui
local GUI2 = Second upgradegui
--etc.

Detector.MouseClick:Connect(function()
GUI1.Transparency = 1
GUI2.Transparency = 0
-- Basically set all GUIs Transparency to 0 that aren't the one the player chose .
end)

This is my idea but im not sure on how to Add more GUI Variables when the user puts down more.

Again i hope this somehow helps you

1 Like

you can add bool values intro Rep storage for each gui and when the player for example click on gui 1 set the value of gui1 to true and when he try to open the other one check if the value of gui1 is true or not if it is then hide gui 1 if not then that means the player didn’t open any other gui’s before, do this for both.

local gui = script.Parent.UpgradeTower
local sound = workspace.error

function onClick(plr)
	if plr.Name == script.Parent.Parent.creator.Value then
		if plr.PlayerGui:FindFirstChild('UpgradeTower') then
			plr.PlayerGui.UpgradeTower:Remove()
		end

		local guicopy = gui:Clone()
		guicopy.Parent = plr.PlayerGui
	else
		sound:Play()
	end
end

script.Parent.ClickDetector.MouseClick:Connect(onClick)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.