Interacting with the Nearest brick not working properly?

I have made a script which should allow me to interact with the nearest brick, however it does not seem to be working as it should as you can see in this gif as it is not interacting with the nearest brick.

I have tried by making a table of the distances I get from the HumanoidRootPart to the bricks within my radius and then getting the shortest distance and match that distance with the bricks. However, this does not seem to be interacting with the nearest brick as you can see in the gif attached. I also am wondering if a more efficient way to do so exists.

Here is my script:

local radius = 8

--Get table with distances
function Get_Distances(player, character)
	local distances = {}
	local models = workspace.Models:GetChildren()
	if #models >= 1 then
		for i,v in pairs(models) do
			local position1 = character.HumanoidRootPart.Position
			local position2 = v.Position
			local distance = (position1 - position2).magnitude
			if distance <= radius then
				table.insert(distances, distance)
				return distances
			end
		end	
	end
end

--Get the nearest Interaction by matching the minimum distances with the table.
function Get_Nearest_Interaction(player, character, distances)
	local models = workspace.Models:GetChildren()
	for i,v in pairs(models) do
		local position1 = character.HumanoidRootPart.Position
		local position2 = v.Position
		local distance = (position1 - position2).magnitude
		if distance == math.min(unpack(distances)) then
			return v
		end
	end
end

--Interacting with the nearest Interaction
game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		spawn(function()
			while wait(0.1) do
				local Nearest_Interaction
				if Get_Distances(player, character) then
					local distances = Get_Distances(player, character)
					Nearest_Interaction = Get_Nearest_Interaction(player, character, distances)
					Nearest_Interaction.BrickColor = BrickColor.Random()
				end
			end
		end)		
	end)
end)

Your function Get_Distances() returns immediately after adding a part to the distances table. This means that the table will only ever have one part, which in this case is the one further from you.

A better way to do this would be to incrementally decrease the radius as you run through parts. This ensures that the final part is the closest. It also means you only need one function to do it.

local Players = game:GetService("Players")
local Models = workspace.Models

--Returns the closest part, or nil if none are in range
function getClosestPart(Player)
	local Range = 8
	local ClosestPart = nil
	local List = Models:GetChildren()
	if #List > 0 then
		for _,Part in pairs(List) do
			local Distance = Player:DistanceFromCharacter(Part.Position) --PS: This function returns 0 if the Character doesn't exist, so exclude 0 as a possibility
			if Distance > 0 and Distance < Range then
				Range = Distance
				ClosestPart = Part
			end
		end
	end
	return ClosestPart
end

--Interacting with the nearest Interaction
Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(function()
		spawn(function()
			while wait(0.1) do
				local ClosestPart = getClosestPart(Player)
				if ClosestPart then
					ClosestPart.BrickColor = BrickColor.Random()
				end
			end
		end)		
	end)
end)
6 Likes

Thanks!