What I want to do is find the lowest value in a distance.
I tried this several times but failed
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.
local RedPart = game.Workspace.RedPart
local PartInMiddle = game.Workspace.PartInMiddle
if (RedPart.Position - PartInMiddle).Magnitude < 2 then
print('In Range')
end
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
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
-- 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.