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)
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
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”
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))