Finding the lowest value in a distance

You can do this:

local Folder = workspace.Folder
local ClosetPart
local Distance = 25
for i,v in pairs(Folder:GetChildren()) do
	if (script.Parent.Position - v.Position).Magnitude <= Distance then
		ClosetPart = v
		Distance = (script.Parent.Position - v.Position).Magnitude -- sets it so we can find the closet part
	end
end
print(ClosetPart)

prints the red part since its the closest
image
image

1 Like

Maybe this:

local PartInMiddle = game.Workspace.PartInMiddle

for _, APart in pairs(Folder:GetChildren()) do
	local PartWithLoestValue = false
	local LowestValue = math.huge
	if (APart.Position - PartInMiddle).Magnitude < 20 then
		print(APart.Name..' Is In Range')
		if APart.Number.Value < LowestValue then
			PartWithLoestValue = APart
			LowestValue = APart.Number.Value
		end
	else
		print(APart.Name..' Is NOT In Range')
	end
	
	print(PartWithLoestValue.Name..' has a value of'..tostring(LowestValue))
end
2 Likes

For the record, @Dan_foodz’s most recent post is the only solution so far which answers the actual question, which was not “how do I find the closest part to a position within a distance”

1 Like

My solution just did, but alright…
It loops through and returns the closest object relative to that part.

Not trying to knock you! Just trying to avoid confusion.

Your solution is good, but it answers this question:

How do I find the closest object to a part?

OP is asking this:

How do I find the object within a certain distance to a part, which has the lowest NumberValue.Value inside it.

@Syntheix, made this script:

Following everything @nicemike40 said:

Wow! Thank you I’m currently testing.

Small correction:

(APart.Position - PartInMiddle).Magnitude

should be

(APart.Position - PartInMiddle.Position).Magnitude

Also, the loop isn’t quite right. Your PartWithLoestValue and LowestValue should be outside the loop.

Fixed:

local PartInMiddle = game.Workspace.PartInMiddle

local PartWithLoestValue = false
local LowestValue = math.huge

for _, APart in pairs(Folder:GetChildren()) do
	
	if (APart.Position - PartInMiddle.Position).Magnitude < 20 then
		print(APart.Name..' Is In Range')
		if APart.Number.Value < LowestValue then
			PartWithLoestValue = APart
			LowestValue = APart.Number.Value
		end
	else
		print(APart.Name..' Is NOT In Range')
	end
end

print(PartWithLoestValue.Name..' has a value of'..tostring(LowestValue))

If there is anything else wrong, someone tell me.

2 Likes

Just tried it. IT WORKS THANK YOU SO MUCH!! :grinning:

@fatih1106, Don’t give me the Solution, everyone here helped out in some way, so don’t give it to me.

Well… What should I do? Don’t give it anyone and credit everyone?

Just give it to @Dan_foodz lol

Against their wishes

Idk, I just know that I and not the only one who deserves this.

@nicemike40, @kinglol123468, @Syntheix, and me @Dan_foodz deserve this.

So, maybe either give no one it, or maybe give this one it… idk.

give this post the Solution, if not for it, I would not know what to do:

Alright then… But thanks to everyone!