Finding Closest part end up with two parts instead of one

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

So I want to get a script where when the E Key is pressed the Closest part to the player will be printed in the output,

  1. What is the issue? Include screenshots / videos if possible!

when i test the script the output comes up with two parts instead of one



in these images the part closest part is Part1 but the script still choses to print the other part even if part1 is closer to the player than part

However when i position the player on the other side of the two parts it ends up working the way i want it to


  1. What solutions have you tried so far?

So i have tried looking at other scripts on finding the closest part but I couldnt seem to find out how to get it to work

-- This is The Script i used
local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)


if input.UserInputType == Enum.UserInputType.Keyboard then
	if input.KeyCode == Enum.KeyCode.E then
		print("E was pressed")
		local player = game.Players.LocalPlayer
			local NearestPart = nil
			local Lenght = nil
			local Folder = workspace.Folder
			local Player = game.Players.LocalPlayer

			wait(0.5)

			for i,v in pairs(Folder:GetChildren()) do
				local LenghtX = Player:DistanceFromCharacter(v.Position)
				if Lenght == nil or LenghtX < Lenght then
					Lenght = LenghtX
				NearestPart = v
				print(NearestPart.name, player:DistanceFromCharacter(NearestPart.Position))
				end
			end
		end
	end
end)

Any Help would be appreciated :smiley:

you are printing the results inside the loop, so before the final results, you are printing them

the getchildren happens to get part before part1, resulting in the above pics.

1 Like

You need to return in order to stop the loop iifc.

-- This is The Script i used
local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.E then
			print("E was pressed")
			local player = game.Players.LocalPlayer
			local NearestPart = nil
			local Lenght = nil
			local Folder = workspace.Folder
			local Player = game.Players.LocalPlayer
			
			wait(0.5)
			
			for i,v in pairs(Folder:GetChildren()) do
				local LenghtX = Player:DistanceFromCharacter(v.Position)
				if Lenght == nil or LenghtX < Lenght then
					Lenght = LenghtX
					NearestPart = v
					print(NearestPart.name, player:DistanceFromCharacter(NearestPart.Position))
					return
				end
			end
		end
	end
end)

Edit: I meant to reply to the OP.

1 Like

no. that will only return the first part, not the cloesest.
since length == nil, it will always be true for the first part, then returning removing the chance of finding the closest part,

              for i,v in pairs(Folder:GetChildren()) do
				local LenghtX = Player:DistanceFromCharacter(v.Position)
				if Lenght == nil or LenghtX < Lenght then
					Lenght = LenghtX
					NearestPart = v
				end
			   end
             print(NearestPart.name, player:DistanceFromCharacter(NearestPart.Position))
1 Like

Oh yeah, I didn’t read the code.

1 Like

Just Tested it out and it works, thanks both of you :smiley: