Check The Closest Part To The Player From The Other Parts

Hello, I am a beginner in scripting and wanted to create a simple system where a player can see which doors they can open. When the player approaches a door, a green highlight shines on it to indicate they are close enough to open it.

The issue I faced is that when I approach two doors, the highlight shines on both of them. I tried to solve this problem by using an array to keep track of which door is illuminated, but it didn’t work. I also attempted to use a Boolean value but that also failed.

Local Script :

local module = require(game.ReplicatedStorage.ModuleScript)
local doors = game.Workspace.Doors:GetChildren()
local player = game.Players.LocalPlayer
local char = player.Character
local humanoidrootpart = char:FindFirstChild("HumanoidRootPart")

local tool = script.Parent

local finalDistance = 0

local distance = 10

local toolEquipped = false

tool.Equipped:Connect(function()
	toolEquipped = true
	for i, v in ipairs(doors) do
		if v:IsA("Model") then
		local door = v.Door
		module.highlight(v, 255, 0, 0)
		local checkLoop = coroutine.wrap(function()
			while wait(.15) do
				finalDistance = (humanoidrootpart.Position - door.Position).Magnitude
					if finalDistance <= 10 and toolEquipped then
					module.highlight(v, 0, 255, 0)
					elseif finalDistance > 10 and toolEquipped  then

					module.highlight(v, 255, 0, 0)
					end
				end
			end)()
		end
	end
end)

tool.Unequipped:Connect(function()
	toolEquipped = false
	for i, v in ipairs(doors) do
		module.highlightDel(v)
	end
end)

local activated = false

tool.Activated:Connect(function()
	for i, v in ipairs(doors) do
		local highlightCheck = v:FindFirstChild("Highlight")
		if highlightCheck and highlightCheck.FillColor == Color3.fromRGB(0, 255, 0) then

			local door = v.Door
			if not activated then
			activated = true
			door.Transparency = .5
			task.wait(2)
			door.Transparency = 0
			activated = false
			end
		end
	end
end)

Module Scirpt :

local module = {
	 highlight = function(v, r, g, b)
		local highlight = Instance.new("Highlight")
		highlight.FillColor = Color3.fromRGB(r, g, b)
		highlight.OutlineColor = Color3.fromRGB(0, 0, 0)
		local highlightClone = highlight:Clone()
		local highlightCheck = v:FindFirstChild("Highlight")
		if highlightCheck then
			highlightCheck:Destroy()
			highlightClone.Parent = v
		else
			
			highlightClone.Parent = v
		end

	end,
	highlightDel = function(v)
		local highlight = v:FindFirstChild("Highlight")
		if highlight then
			highlight:Destroy()
		end
	end,
}

return module

have you tried reducing the finaldistance argument from 10 to something less? 10 is a pretty big gap bro

if finalDistance <= 10 and toolEquipped then
		module.highlight(v, 0, 255, 0)

It is working, but I want the tool to work remotely, and when I do this, this problem occurs.

you can try to check which door is the closest to the player

I tried but failed. Do you have any idea how I can do this?

local closestPart
local closestDistance = math.huge

for _, part in parts do
   local distance = (part.Position - player.Character:GetPivot().Position).Magnitude
   if distance <= closestDistance then
      closestPart = part
      closestDistance = distance
   end
end

do whatever with
closestPart here

1 Like

ok I fixed it

local doors = game.Workspace.Doors:GetChildren()
local player = game.Players.LocalPlayer
local char = player.Character
local humanoidRootPart = char:FindFirstChild("HumanoidRootPart")

local tool = script.Parent

local toolEquipped = false

local closestPart = nil
local currentHighlights = {}

tool.Equipped:Connect(function()
	toolEquipped = true

	for i, v in ipairs(doors) do
		if v:IsA("Model") then
			local door = v.Door

			local highlight = Instance.new("Highlight")
			highlight.FillColor = Color3.fromRGB(255, 0, 0)
			highlight.Parent = v

			currentHighlights[v] = highlight
		end
	end

	local function updateHighlights()
		while toolEquipped do

			local closestDistance = math.huge

			for _, part in ipairs(doors) do
				local distance = (part.Door.Position - humanoidRootPart.Position).Magnitude

				if distance <= 10 then
					if distance < closestDistance then
						closestDistance = distance
						closestPart = part
					end
				end
			end

			for part, highlight in pairs(currentHighlights) do
				if part == closestPart then
					highlight.FillColor = Color3.fromRGB(0, 255, 0)
				else
					highlight.FillColor = Color3.fromRGB(255, 0, 0)
				end
			end

			wait(0.15)
		end
	end

	updateHighlights()
end)

tool.Unequipped:Connect(function()
	toolEquipped = false

	for _, highlight in pairs(currentHighlights) do
		highlight:Destroy()
	end
	currentHighlights = {}
end)

tool.Activated:Connect(function()
	if closestPart then
		local door = closestPart.Door
		door.Transparency = 0.5
		task.wait(2)
		door.Transparency = 0
	end
end)

thx for your help :heart: @BlueBloxBlast

1 Like

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