Finding the lowest value in a distance

What I want to do is find the lowest value in a distance.

I tried this several times but failed

resim_2022-01-09_210811
Pastel blue is parent of a value with number 1, isn’t in the distance
Red is parent of a value with number 2, is in the distance
Green is parent of a value with number 3, is in the distance

The thing I want is to detect red, not blue.

Purple is distance.

I have a collection of Parts in a Folder. Each Part has a NumberValue.

I have another part, called “Center”.

There is a distance of 20

There are 2 parts in distance
There is 1 part outside of distance

All parts have number values inside them. Pastel blue 1, Red 2, Green 3.

Since pastel blue isn’t in the distance I want script to detect red because it has the lowest value in the distance.

Edit: All parts have the same name and they are in a folder.

1 Like

you can do this:

local RedPart = game.Workspace.RedPart

local PartInMiddle = game.Workspace.PartInMiddle

if (RedPart.Position - PartInMiddle).Magnitude < 2 then

print('In Range')

end

Hope this helped :slight_smile:

I’m sorry I forgot to say. These parts are in a folder. Is there a way to do this using lua Folder:GetChildren()

Like this?

local PartInMiddle = game.Workspace.PartInMiddle

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

I think by “distance” you mean “Region3” or something similar.

If not, how is your purple area defined?

Well all parts have value in them. And I’m trying to find the lowest value in the distance.

I meant magnitude, purple thing shows the distance.

So, like a number value, what is the value called in them?

Oh, I see. Distances are usually circles :slight_smile:

Yes, they’re number value.

Pastel blue has value 1,
Red has 2,
Green has 3

and I want to script to detect red because it has the lowest value in the distance.

You want to get Red?

Why would this not work?

I want to get red because it has the lowest value.

I don’t want red specifically.

Being brutually honest, I really hate all the wording.
Back on to the topic, you want to get the closest object in a region to the center? Or just the closest object relative to another object.

-- Relative to a specific object
local object = [..]
local objects = [..]:GetChildren()

local closestobject, closestdist = nil, math.huge()
for _, v in pairs(objects) do
     if (object.Position - v.Position).Magnitude < closestdist then
          closestdist = (object.Position - v.Position).Magnitude 
          closestobject = v
     end 
end

I am a bit confused, do you want to go though all of the parts in the folder, and find the one with the lowest distance, then that would be red?

I think this is a rephrasing of OPs question to help out:

I have a collection of Parts in a Folder. Each Part has a NumberValue.

I have another part, called “Center”.

How do I find the single Part which:

  1. Is within X studs of Center
  2. Has the lowest NumerValue.Value of all the Parts also within X studs of Center

Yes I’m kinda bad at this.

There is a distance of 20

There are 2 parts in distance
There is 1 part outside of distance

All parts have number values inside them. Pastel blue 1, Red 2, Green 3.

Since pastel blue isn’t in the distance I want script to detect red because it has the lowest value in the distance.

Also they have same name and they are in a folder.

Yes, I’ll edit now. Thank you.

-- Relative to a specific object
local object = [..]
local objects = [..]:GetChildren()

local closestobject, closestdist = nil, math.huge
for _, v in pairs(objects) do
     if (object.Position - v.Position).Magnitude <= closestdist then
          closestdist = (object.Position - v.Position).Magnitude 
          closestobject = v
     end 
end

You’ll have to modify the code to your needs but that’s basically it.

1 Like