How i can fix my code to search the door more near of the HumanoidRootPart?

I want to make me show a gui when I go to the doors or windows

but, the code looks for a door or window at random then only the gui appears on a door or window, I would like to know how I can repair. the code is here:

(i tried to fix it but i dont know how)

while wait() do
	for _,Magnitude in pairs(game.Workspace.GameStuffs.MagnitudeStuffs:GetChildren()) do
	local Mag1 = (Magnitude.Center1.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
	local Mag2 = (Magnitude.Center2.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
	print(Mag1)
	print(Mag2)
		if Mag1 <= Magnitude.Range.Value and Magnitude.Center1.OnOrOff.Value == true or Mag2 <= Magnitude.Range.Value and Magnitude.Center2.OnOrOff.Value == true then
			script.Parent.TextLabel.Visible = true
			script.Parent.TextLabel.Text = "'E' to interact"
			print("working")
			break
		else
			print("stop")
			script.Parent.TextLabel.Visible = false
			break
		end
	end	
end

Here is a gif with the problem:
https://gyazo.com/5bd8db2c6245082bef7989aef9cc26cc

1 Like

Perhaps you could let us see the prints output.

I updated it now i added a video

can you send us a screenshot of the explorer with the script?

This is only a Folder in workspace with the stuffs and in the playergui is a gui with the local script (magnitude script)

can you send the explorer screenshot with the gui and this script, please?

I fixed the code now that is finished

hre is the code if someone need fix the same problem:

while wait() do
	for _,Magnitude in pairs(game.Workspace.GameStuffs.MagnitudeStuffs:GetChildren()) do
				local Mag1 = (Magnitude.Center1.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
				local Mag2 = (Magnitude.Center2.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
				if Mag1 <= Magnitude.Range.Value and Magnitude.Center1.OnOrOff.Value == true or Mag2 <= Magnitude.Range.Value and Magnitude.Center2.OnOrOff.Value == true then
						script.Parent.TextLabel.Visible = true
			script.Parent.TextLabel.Text = "'E' to interact"
			print("working")
			break
		elseif Mag1 > Magnitude.Range.Value and Mag2 > Magnitude.Range.Value then --Problem
			print("stop")
			script.Parent.TextLabel.Visible = false
				end
	end
	end