Advertisement
Anukun_Lucifer

MoveToWayPointsReturn

Aug 24th, 2024
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.13 KB | Gaming | 0 0
  1. local wps = script.Parent.Parent.Wps -- เข้าถึงโฟลเดอร์ที่เก็บ Waypoints
  2. local track = {} -- สร้างตารางว่างเพื่อเก็บ Waypoints
  3.  
  4. -- รวบรวม Waypoints จากโฟลเดอร์และเพิ่มลงในตาราง track
  5. for _, wp in ipairs(wps:GetChildren()) do
  6.     table.insert(track, wp)
  7. end
  8.  
  9. -- จัดเรียงลำดับ Waypoints ตามตัวเลขในชื่อ
  10. table.sort(track, function(a, b)
  11.     return tonumber(a.Name:match("%d+")) < tonumber(b.Name:match("%d+"))
  12. end)
  13.  
  14. local wpIdx = 1 -- กำหนดตัวแปรเริ่มต้นสำหรับการเคลื่อนที่ไปยัง Waypoint แรก
  15. local char = script.Parent -- เก็บข้อมูลของตัวละคร
  16. local hum = char:WaitForChild("Humanoid") -- ค้นหา Humanoid ของตัวละครเพื่อใช้ในการเคลื่อนที่
  17. local forward = true -- กำหนดทิศทางเริ่มต้นในการเคลื่อนที่ให้เป็นไปข้างหน้า
  18.  
  19. -- ฟังก์ชันสำหรับการเคลื่อนที่ไปยัง Waypoint ถัดไป
  20. local function moveToNextWaypoint()
  21.     hum:MoveTo(track[wpIdx].Position) -- เคลื่อนที่ไปยังตำแหน่งของ Waypoint ที่กำหนด
  22. end
  23.  
  24. -- เมื่อการเคลื่อนที่ไปยัง Waypoint ปัจจุบันเสร็จสิ้น
  25. hum.MoveToFinished:Connect(function()
  26.     -- หยุดรอ 10 วินาทีหากถึงจุดแรกหรือจุดสุดท้าย
  27.     if (wpIdx == 1 or wpIdx == #track) then
  28.         task.wait(10)
  29.     end
  30.  
  31.     -- ตรวจสอบทิศทางและปรับ wpIdx เพื่อเคลื่อนที่ไปยัง Waypoint ถัดไปหรือลดลง
  32.     if forward then
  33.         if wpIdx == #track then
  34.             forward = false -- หากถึงจุดสุดท้าย เปลี่ยนทิศทางเป็นถอยหลัง
  35.             wpIdx = wpIdx - 1 -- เคลื่อนที่ไปยัง Waypoint ก่อนหน้า
  36.         else
  37.             wpIdx = wpIdx + 1 -- เคลื่อนที่ไปยัง Waypoint ถัดไป
  38.         end
  39.     else
  40.         if wpIdx == 1 then
  41.             forward = true -- หากถึงจุดแรก เปลี่ยนทิศทางเป็นไปข้างหน้า
  42.             wpIdx = wpIdx + 1 -- เคลื่อนที่ไปยัง Waypoint ถัดไป
  43.         else
  44.             wpIdx = wpIdx - 1 -- เคลื่อนที่ไปยัง Waypoint ก่อนหน้า
  45.         end
  46.     end
  47.  
  48.     -- เรียกฟังก์ชัน moveToNextWaypoint() เพื่อเคลื่อนที่ไปยัง Waypoint ต่อไป
  49.     moveToNextWaypoint()
  50. end)
  51.  
  52. moveToNextWaypoint() -- เริ่มต้นเคลื่อนที่ไปยัง Waypoint แรก
Tags: Roblox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement