การประกาศและเรียกใช้ Function

 สร้าง Function เพื่อหาวัตถุที่อยู่ใกล้สุด




local parts = {Vector3.new(5, 3, 4), Vector3.new(1, 2, 5), Vector3.new(-7, 0, 9)} -- Add more part here

Function FindNearestPart(me, others)

local nearestDistance = math.huge -- Start with large number
local nearestPart = nil -- nil is empty for object's reference

for _, part in others do -- For each loop for table

if (part - me).Magnitude < nearestDistance then

nearestDistance = (part - me).Magnitude
nearestPart = part

end -- end if

end -- end loop

return nearestPart -- return result

end

print(FindNearestPart(Vector3.new(0, 0, 0), parts))

ความคิดเห็น