helllo. i am trying to get the closest part to the First_Spawn. the First_Spawn is in the same folder than all the others. somehow i sapwn on a distanced seat sometimes. how to fix?
for a, b in ipairs(ChosenHouse.SEATS:GetChildren()) do
if b == First_Spawn then continue end
local dist = (b.Position-b.Position).Magnitude
if dist < closestDist then
closestPart = b; closestDist = dist end
end
local ClosestPart = nil
for i, parts in pairs(ChosenHouse.SEATS:GetChildren()) do
if ClosestPart ~= nil then
if parts.Magitude > ClosestPart.Magitude then
ClosestPart = parts
end
else
ClosestPart = parts
end
end
This will get all parts in the folder, if the ClosestPart variable is not nil, then it will check if parts.Magitude is bigger then ClosestPart.Magitude, if it is then ClosestPart will update to the part selection, elseif it is nil, then ClosestPart will automatically turn into parts.
now i only need to check that the closestpart isnt the first spawn and if it is, continue to search for the closest part. how i do so? the one that answers this question gets the solution
What are closestPart and closestDist set to before the loop? You should set them both to nil and then when the loop starts make sure first part gets set as the closest:
if not closestDist then
closestPart = b
closestDist = dist
continue
end
local FirstS = First_Spawn.Position
local ClosestD = math.huge
local ClosetP = First_Spawn
for a,b in ipairs(ChosenHouse.SEATS:GetChildren()) do
if b == First_Spawn then
continue
end
local dis = (FirstS.Position - b.Position).Magnitude
if ClosestD > dis then
ClosestP = b
ClosestD = dis
end
end