Advertisement
IsntWillYT

NPC Chase Tutorial

Jan 10th, 2023
1,709
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 1 0
  1. local npc = script.Parent
  2. local npcHumanoid = script.Parent:WaitForChild("Humanoid")
  3. local npcHRP = script.Parent:WaitForChild("HumanoidRootPart")
  4.  
  5. npcHRP:SetNetworkOwner(nil)
  6.  
  7. local players = game:GetService("Players")
  8. local runService = game:GetService("RunService")
  9.  
  10. local damage = 10
  11.  
  12. local function Chase()
  13. local target = nil
  14. local allPlayers = players:GetPlayers()
  15.  
  16. for i, player in pairs(allPlayers) do
  17. local character = player.Character
  18.  
  19. if character then
  20. local playerHRP = character:FindFirstChild("HumanoidRootPart")
  21.  
  22. if playerHRP then
  23.  
  24. if target then
  25. if (npcHRP.Position - target.Position).Magnitude > (npcHRP.Position - playerHRP.Position).Magnitude then
  26.  
  27. target = playerHRP
  28. end
  29. else
  30. target = playerHRP
  31. end
  32. end
  33. end
  34. end
  35.  
  36. if target then
  37. npcHumanoid:MoveTo(target.Position)
  38. end
  39. end
  40.  
  41. local debounce = false
  42.  
  43. npcHRP.Touched:Connect(function(hit)
  44. local playerHumanoid = hit.Parent:FindFirstChild("Humanoid")
  45.  
  46. if playerHumanoid and debounce == false then
  47. debounce = true
  48. playerHumanoid.Health -= damage
  49.  
  50. wait(1)
  51.  
  52. debounce = false
  53. end
  54. end)
  55.  
  56. runService.Stepped:Connect(Chase)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement