How can I find multiple closest parts to a player?

Yes you can.

If you don’t want to destroy the part Change:

v:Destroy()

To:

print(v.Name)

Yes, I understand, but I want it so you can see if multiple parts are the closest instead of one. I have 50 parts in workspace and I want to see which ones are the 2 closest.

I have already told you that you can print multiple parts that are closest to the humanoid root part character.

k found out how

local PartsToFind = 3
game.Players.PlayerAdded:Connect(
	function(player)
		player.CharacterAdded:Connect(
			function(Character)
				local humanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
				local dictionary = {}
				for i, v in pairs(workspace:GetChildren()) do
					if v:IsA("Part") and string.sub(v.Name, 1, 4) == "Part" then
						dictionary[v.Name] = (humanoidRootPart.Position - v.Position).Magnitude
					end
				end
				
				wait(3)
				local array = {}
				for _, value in pairs(dictionary) do
					table.insert(array, tonumber(value))
				end
				table.sort(array)
				
				for i = 1, PartsToFind do
					local key = array[i]
					for i, v in pairs(dictionary) do
						print("Is " .. key .. " equivalent to " .. v)
						if v == key then
							workspace[i].BrickColor = BrickColor.new("Pink")
						end
					end
				end
			end
		)
	end
)

2 Likes