local Players = game:GetService("Players")

local P = game.Players.LocalPlayer

local Mouse = P:GetMouse()

local Models = workspace

function getClosestPart(Player)

	local Range = 18

	local ClosestPart = nil

	local List = Models:GetChildren()

	if #List > 0 then

		for _,Part in pairs(List) do

			if Part:IsA("BasePart") then

				
local Distance = (game.Players.LocalPlayer.Character.Head.Position - Part.Position).Magnitude 

					if Distance > 0 and Distance < Range then

						Range = Distance

						ClosestPart = Part

						wait()

						print(ClosestPart.Name)			

					end

			else

			end

		end

	end

	return ClosestPart

end

at the Print(ClosestPart.Name) its printing multiple parts, not sure what to do to fix.

2 comments

0

Put 2 base part in the new game (workspace) and then insert this script, does it prints multiple parts?

0

it's simply iterating through the loop. It keeps updating the closest part again and again and eventually returns the actual closest part. Just move your print statement outside of the loop.