GUI only showing for one door

I’m creating a door system that shows a ScreenGui when a player is within a certain radius of the door. I am currently using a LocalScript in StarterPlayerScripts that loops and checks a players magnitude to every door, and then determines if it is less than 10 studs away from the player. However, the script is only working for one of the doors and none of the others, and it is always the same door. I have tried adding a variable that determines whether a door has been found that is less than 10 studs away, but that hasn’t seemed to fix the problem.

local Doors = workspace:WaitForChild("Compound-8"):WaitForChild("Doors")
local plr = game.Players.LocalPlayer
local ConfirmationTrue = plr.PlayerGui:WaitForChild("Interactions"):WaitForChild("Door"):WaitForChild("DoorConfirmationTrue")
local DoorInteraction = plr.PlayerGui:WaitForChild("Interactions"):WaitForChild("Door"):WaitForChild("DoorInteraction")
local RunService = game:GetService("RunService")

wait(2)

RunService.Heartbeat:Connect(function()
	local found
	for i,v in pairs(Doors:GetChildren()) do		
			local char = plr.Character
			local magnitude = (v.Door.Position - char:WaitForChild("Head").Position).Magnitude
		if magnitude <= 10  then
			found = true
			if ConfirmationTrue.Visible == false then
				if found == true then
					DoorInteraction.Visible = true
				end
				end
			end
		if magnitude >= 10  then
			found = false
			DoorInteraction.Visible = false
		end
	end
end)

This is because when you’re walking to one door, that door checks the magnitude and tries to make the UI visible because you’re close to that one, but the other ones are not detecting that you’re close to them, making them turn it off, so you basically have an endless loop of the one you’re close to turning it on and the one you’re far away from turning it off.

How would I go about fixing this? The explanation is clear im just not sure how I would fix it.

Try breaking the loop when the magnitude condition is met. Also, I would change this part of your code and utilize an else statement instead of two ifs.

RunService.Heartbeat:Connect(function()
	local found
	for i,v in pairs(Doors:GetChildren()) do		
		local char = plr.Character
		local magnitude = (v.Door.Position - char:WaitForChild("Head").Position).Magnitude
		if magnitude <= 10  then
		    DoorInteraction.Visible = true
            break
        else
            DoorInteraction.Visible = false
            break
		end 
	end
end)

I did think of this earlier, but when you break this it breaks the whole RunService function.

Fixed it.

local function CheckForDoor()
	for i,v in pairs(Doors:GetChildren()) do		
		local char = plr.Character
		local magnitude = (v.Door.Position - char:WaitForChild("Head").Position).Magnitude
		if magnitude <= 10  then
			if ConfirmationTrue.Visible == true then
				DoorInteraction.Visible = true
				break
			end

		else
			DoorInteraction.Visible = false
		end 
	end
end

RunService.Heartbeat:Connect(CheckForDoor)
if magnitude > 10 and found == false then

Didn’t catch that sorry. I don’t have access to a code editor atm so I wasn’t able to test it. Glad I could help, happy coding!

1 Like