Advertisement
SimTek

AITools

Feb 26th, 2024
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. local aiLib = {}
  2. local players = game:GetService("Players")
  3. local params = RaycastParams.new()
  4. params.FilterDescendantsInstances = { workspace.Zombies }
  5. params.FilterType = Enum.RaycastFilterType.Exclude
  6.  
  7. function aiLib:findNearestPlayersHrp(aiNpc, distOfInt)
  8. local closestHrp = nil
  9. local aiNpcHrp = aiNpc:FindFirstChild("HumanoidRootPart")
  10. for i, v in pairs(players:GetChildren()) do
  11. local char = v.Character
  12. if char then
  13. local hrp = char:FindFirstChild("HumanoidRootPart")
  14. if hrp and aiNpcHrp then
  15. local diff = (hrp.Position - aiNpcHrp.Position)
  16. local dist = diff.Magnitude
  17. if dist < distOfInt then
  18. local result = workspace:Raycast(aiNpcHrp.Position,
  19. diff, params)
  20. if result and result.Instance.Parent.Name == char.Name then
  21. print("closest player = ", v.Name)
  22. distOfInt = dist
  23. closestHrp = hrp
  24. end
  25. end
  26. end
  27. end
  28. end
  29. return closestHrp
  30. end
  31.  
  32. return aiLib
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement