Advertisement
1m1m0

Erik.ai Pathfinder

Jan 1st, 2024
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | Source Code | 0 0
  1. -- Erik's Companion Mode [Dec. 28 2023 Edition] --
  2.  
  3. local SearchDistance = 2500
  4.  
  5. function getHumanoid(model)
  6.     for _, v in pairs(model:GetChildren()) do
  7.         if v:IsA'Humanoid' then
  8.             return v
  9.         end
  10.     end
  11. end
  12.  
  13. local erikAI = script.Parent
  14. local human = getHumanoid(erikAI)
  15. local hroot = erikAI.HumanoidRootPart
  16. local zspeed = hroot.Velocity.magnitude
  17.  
  18. local pfs = game:GetService("PathfindingService")
  19.  
  20. function GetPlayerNames()
  21.     local players = game:GetService('Players'):GetChildren()
  22.     local name = nil
  23.     for _, v in pairs(players) do
  24.         if v:IsA'Player' then
  25.             name = tostring(v.Name)
  26.         end
  27.     end
  28.     return name
  29. end
  30.  
  31. function GetPlayersBodyParts(t)
  32.     local torso = t
  33.     if torso then
  34.         local figure = torso.Parent
  35.         for _, v in pairs(figure:GetChildren()) do
  36.             if v:IsA'Part' then
  37.                 return v.Name
  38.             end
  39.         end
  40.     else
  41.         return "HumanoidRootPart"
  42.     end
  43. end
  44.  
  45. function GetTorso(part)
  46.     local chars = game.Workspace:GetChildren()
  47.     local torso = nil
  48.     for _, v in pairs(chars) do
  49.         if v:IsA'Model' and v ~= erikAI and v.Name == GetPlayerNames() then
  50.             local charRoot = v:FindFirstChild'HumanoidRootPart'
  51.             if (charRoot.Position - part).magnitude < SearchDistance then
  52.                 torso = charRoot
  53.             end
  54.         end
  55.     end
  56.     return torso
  57. end
  58.  
  59. for _, erikAIparts in pairs(erikAI:GetChildren()) do
  60.     if erikAIparts:IsA'Part' then
  61.         erikAIparts.Touched:Connect(function(p)
  62.             if p.Parent.Name == GetPlayerNames() and p.Parent.Name ~= erikAI.Name then
  63.                 local target = p.Parent
  64.                 local targetHuman = getHumanoid(target)
  65.             end
  66.         end)
  67.     end
  68. end
  69.  
  70. local path
  71. local waypoint
  72. local oldpoints
  73. local isWandering = 0
  74.  
  75. while wait() do
  76.     local targetTorso = GetTorso(hroot.Position)
  77.     if targetTorso ~= nil then
  78.         isWandering = 1
  79.         local function checkw(t)
  80.             local ci = 3
  81.             if ci > #t then
  82.                 ci = 3
  83.             end
  84.             if t[ci] == nil and ci < #t then
  85.                 repeat
  86.                     ci = ci + 1
  87.                     wait()
  88.                 until t[ci] ~= nil
  89.                 return Vector3.new(1, 0, 0) + t[ci]
  90.             else
  91.                 ci = 3
  92.                 return t[ci]
  93.             end
  94.         end
  95.  
  96.         path = pfs:FindPathAsync(hroot.Position, targetTorso.Position)
  97.         waypoint = path:GetWaypoints()
  98.         oldpoints = waypoint
  99.         local connection;
  100.  
  101.         local direct = Vector3.FromNormalId(Enum.NormalId.Front)
  102.         local ncf = hroot.CFrame * CFrame.new(direct)
  103.         direct = ncf.p.unit
  104.         local rootr = Ray.new(hroot.Position, direct)
  105.         local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)
  106.  
  107.         if path and waypoint or checkw(waypoint) then
  108.             if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
  109.                 human:MoveTo(checkw(waypoint).Position)
  110.                 human.Jump = false
  111.             end
  112.  
  113.             if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
  114.                 human.Jump = true
  115.                 connection = human.Changed:Connect(function()
  116.                     human.Jump = true
  117.                 end)
  118.                 human:MoveTo(checkw(waypoint).Position)
  119.             else
  120.                 human.Jump = false
  121.             end
  122.  
  123.             hroot.Touched:Connect(function(p)
  124.                 local bodypartnames = GetPlayersBodyParts(targetTorso)
  125.                 if p:IsA'Part' and not p.Name == bodypartnames and phit and phit.Name ~= bodypartnames and phit:IsA'Part' and rootr:Distance(phit.Position) < 5 then
  126.                     connection = human.Changed:Connect(function()
  127.                         human.Jump = true
  128.                     end)
  129.                 else
  130.                     human.Jump = false
  131.                 end
  132.             end)
  133.  
  134.             if connection then
  135.                 connection:Disconnect()
  136.             end
  137.  
  138.         else
  139.             for i = 3, #oldpoints do
  140.                 human:MoveTo(oldpoints[i].Position)
  141.             end
  142.         end
  143.     elseif targetTorso == nil then
  144.         path = nil
  145.         waypoint = nil
  146.         human.MoveToFinished:Wait()
  147.     end
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement