Door Gui script cause button gui to blink non-stop

So I’m trying to create a door gui script that allows the player to view a button prompt when within a certain number of studs from the door, however when near the door, the gui blinks non-stop. I figured the issue probably lies with my debounces but I have no idea how to fix it. Here’s my script :

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input,Istyping)
   if Istyping then
   	return
   
   elseif Input.KeyCode == Enum.KeyCode.E then
   	DoorOpener:FireServer()
   end
end)

local Insight = false

while wait() do
   if Insight == false then
   	Insight = true
   	local ButtonClone = script.Parent:Clone()
   	ButtonClone.Parent = workspace.CurrentCamera
   	ButtonClone.Adornee = workspace.Door
   	ButtonClone.TextLabel.Visible = true
   
   elseif Insight == true then
   	Insight = false
   	if workspace.CurrentCamera:FindFirstChild("Button")then
   		workspace.CurrentCamera.Button:remove()
   		
   		end
   	end
   	
   end

I think it’s because you do it in a while loop, so it adds the gui then removes it almost instantly

Yeah the problem is with the debounce, what’s happening is your creating the button, and setting Insight to true. The next frame, since it’s a loop, the script is detecting that Insight is now false and firing your remove function. And that process repeats going back and forth, causing blinking. The way I’d say to fix it, is have the part of code that sets the debounce in a separate function. (Not inside the same loop)

e.g.

while wait() do
   if Insight == false then
   	Insight = true
   	local ButtonClone = script.Parent:Clone()
   	ButtonClone.Parent = workspace.CurrentCamera
   	ButtonClone.Adornee = workspace.Door
   	ButtonClone.TextLabel.Visible = true
   end
end

--have a function here that detecs when the player is out of range of the button
--since your using studs, just detect when the player is farther away than the maximum you want
--(I'd recommend the same thing to fire the first function also)
   if Insight then --you don't need to have the "== true"
      if workspace.CurrentCamera:FindFirstChild("Button") then
         workspace.CurrentCamera.Button:remove()
      end
   end
end
1 Like

Could you clarify what exactly should the distance detection function be? I had the Insight variable defined as false, why do I not need the “==true”?

Also when I place the billboard Gui into the starter gui, the whole thing just moves to a different location instead of the door.

You can have the == true, it’s just with Luau, you don’t need to have it. it still works either way.
And the distance function would be the distance of studs from the players HumanoidRootPart to the door.

Leave the Gui inside the part. Since it’s not a ScreenGui, you don’t need to put it inside StarterGui.

When I put the billboard gui inside the door, the door stops working.