Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local wps = script.Parent.Parent.Wps -- เข้าถึงโฟลเดอร์ที่เก็บ Waypoints
- local track = {} -- สร้างตารางว่างเพื่อเก็บ Waypoints
- -- รวบรวม Waypoints จากโฟลเดอร์และเพิ่มลงในตาราง track
- for _, wp in ipairs(wps:GetChildren()) do
- table.insert(track, wp)
- end
- -- จัดเรียงลำดับ Waypoints ตามตัวเลขในชื่อ
- table.sort(track, function(a, b)
- return tonumber(a.Name:match("%d+")) < tonumber(b.Name:match("%d+"))
- end)
- local wpIdx = 1 -- กำหนดตัวแปรเริ่มต้นสำหรับการเคลื่อนที่ไปยัง Waypoint แรก
- local char = script.Parent -- เก็บข้อมูลของตัวละคร
- local hum = char:WaitForChild("Humanoid") -- ค้นหา Humanoid ของตัวละครเพื่อใช้ในการเคลื่อนที่
- local forward = true -- กำหนดทิศทางเริ่มต้นในการเคลื่อนที่ให้เป็นไปข้างหน้า
- -- ฟังก์ชันสำหรับการเคลื่อนที่ไปยัง Waypoint ถัดไป
- local function moveToNextWaypoint()
- hum:MoveTo(track[wpIdx].Position) -- เคลื่อนที่ไปยังตำแหน่งของ Waypoint ที่กำหนด
- end
- -- เมื่อการเคลื่อนที่ไปยัง Waypoint ปัจจุบันเสร็จสิ้น
- hum.MoveToFinished:Connect(function()
- -- หยุดรอ 10 วินาทีหากถึงจุดแรกหรือจุดสุดท้าย
- if (wpIdx == 1 or wpIdx == #track) then
- task.wait(10)
- end
- -- ตรวจสอบทิศทางและปรับ wpIdx เพื่อเคลื่อนที่ไปยัง Waypoint ถัดไปหรือลดลง
- if forward then
- if wpIdx == #track then
- forward = false -- หากถึงจุดสุดท้าย เปลี่ยนทิศทางเป็นถอยหลัง
- wpIdx = wpIdx - 1 -- เคลื่อนที่ไปยัง Waypoint ก่อนหน้า
- else
- wpIdx = wpIdx + 1 -- เคลื่อนที่ไปยัง Waypoint ถัดไป
- end
- else
- if wpIdx == 1 then
- forward = true -- หากถึงจุดแรก เปลี่ยนทิศทางเป็นไปข้างหน้า
- wpIdx = wpIdx + 1 -- เคลื่อนที่ไปยัง Waypoint ถัดไป
- else
- wpIdx = wpIdx - 1 -- เคลื่อนที่ไปยัง Waypoint ก่อนหน้า
- end
- end
- -- เรียกฟังก์ชัน moveToNextWaypoint() เพื่อเคลื่อนที่ไปยัง Waypoint ต่อไป
- moveToNextWaypoint()
- end)
- moveToNextWaypoint() -- เริ่มต้นเคลื่อนที่ไปยัง Waypoint แรก
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement