Advertisement
The_Epic

FollowingScript

Mar 22nd, 2019
3,371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.03 KB | None | 0 0
  1. --Make by The epic
  2. local DetectDistance = 20 -- ระยะตรวจจับ เปลี่ยนเอาเองได้
  3. local MaxSpeed = 10 -- ความเร็วของ NPC เปลี่ยนเองได้
  4.  
  5. local Enemy = script.Parent -- กำหนดตัวแปรแทนศัตรู
  6. local BasePos = Enemy.Torso.Position
  7. Enemy.Humanoid.WalkSpeed = MaxSpeed -- ตั้งค่าความเร็ว
  8.  
  9. while true do
  10.     wait(0.1) -- กันเกมพัง
  11.     for i, Player in pairs(game.Players:GetPlayers()) do -- ไล่ดูผู้เล่นทีละคน
  12.         if game.Workspace:FindFirstChild(Player.Name) == nil then -- ถ้าหาผู้เล่นไม่เจอใน Workspace
  13.             print("Not found the player who is selected.")
  14.             break -- กลับไปไล่หาคนอื่น
  15.         end
  16.        
  17.         local DisBetweenPlyEnemy =  Player:DistanceFromCharacter(Enemy.Torso.Position)-- ตั้งตัวแปรเก็บค่าระยะห่างระหว่าง ศัตรู กับ ผู้เล่น
  18.        
  19.         while DisBetweenPlyEnemy < DetectDistance do -- ถ้าระยะใกล้กว่าระยะตรวจจับ
  20.             wait(0.1) -- กันเกมพัง
  21.             local PlayerPos = game.Workspace[Player.Name].HumanoidRootPart.Position -- หาพิกัดผู้เล่น
  22.             DisBetweenPlyEnemy =  Player:DistanceFromCharacter(Enemy.Torso.Position)-- ตั้งตัวแปรเก็บค่าระยะห่างระหว่าง ศัตรู กับ ผู้เล่น ซ้ำ
  23.             Enemy.Humanoid:MoveTo(PlayerPos)-- เดินตามผู้เล่น
  24.             if DisBetweenPlyEnemy > DetectDistance then
  25.                 Enemy.Humanoid:MoveTo(Enemy.Torso.Position)-- ทำให้หยุดเดิน
  26.                 print(Player.Name.." is too far to catch him")
  27.                 wait(2)
  28.                 Enemy.Humanoid:MoveTo(BasePos)
  29.                 break -- กลับไปหาคนอื่นแทน
  30.             end
  31.         end
  32.     end
  33. end
  34. --Make by The epic
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement