Player somehow still has gui

So in this code:

script.Parent.Touching.Touched:Connect(function(hit)
	if not deb then
		if hit.Parent:FindFirstChild("Humanoid") then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			local plrGui = plr:WaitForChild("PlayerGui")
			
			local gui = plrGui:FindFirstChild("ExchangeGui")
			
			deb = true
			
			if gui == nil then
				local clone = script.ExchangeGui:Clone()
				clone.Parent = plrGui
				wait(1)
				deb = false
			elseif gui ~= nil then
				warn("Player still has gui")
				deb = false
			end
		end
	end
end)

You get a GUI if you touch it but if you already have a GUI then you don’t get one. When I got the warn statement, I was confused because in explorer there wasn’t a GUI.

So should I change the if statement?

print(not not gui) -- prints true even though I don't have the gui anymore

Here is the code for the GUI:

local remote = game.ReplicatedStorage.Remotes.ExchangeCurrency

for i, ui in pairs(script.Parent:GetChildren()) do
	if ui.ClassName == "TextButton" then
		ui.Activated:Connect(function()
			remote:FireServer(ui.Name)
			script.Parent.Parent:Destroy() -- script.Parent.Parent is the gui
		end)
	end
end

script.Parent.Parent.Exit.Activated:Connect(function()
	script.Parent.Parent:Destroy() -- script.Parent.Parent is the gui
end)

You are removing the gui using a local script. This means only the client will see the gui being deleted, but the server won’t detect any change.

Why do you need to destroy the gui though? You could set Enabled or Visible properties of the gui object to false. Or delete it from the server when calling remote:FireServer(ui.Name) (don’t forget to do so when “Exit” is activated too, maybe changing the arguments).

1 Like