Advertisement
SpacecowboyHX

Roblox Autofight (Pathfinding)

Aug 4th, 2020 (edited)
2,315
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.69 KB | None | 1 0
  1. --[[
  2. /////////AutoFight///////
  3. ////Made by Indencito////
  4. ]]
  5. local lp = game.Players.LocalPlayer
  6. local players = game.Players:GetChildren()
  7. local availPlayers = {}
  8. local currentOuts = {}
  9. local outlined = false
  10. local cam = workspace.CurrentCamera
  11. local db = false
  12. local gotoRefresh = 0.5
  13. local pf = false
  14. local params = {
  15.     ["AgentHeight"] = 5;
  16.     ["AgentRaduis"] = 7;
  17.     ["AgentCanJump"] = true
  18. }
  19. function Goto(player,pos)
  20.     local char
  21.     if player~=nil then
  22.         char = player.Character
  23.     end
  24.     local hum
  25.     if char then
  26.         hum = char:FindFirstChild("Humanoid")
  27.     end
  28.     if char and hum then
  29.         local parts = {}
  30.         local health = hum.Health
  31.         local paths = game:GetService("PathfindingService")
  32.         local path = paths:CreatePath(params)
  33.         local health2 = lp.Character.Humanoid.Health
  34.         if health>0 and health2>0 then
  35.             path:ComputeAsync(char.PrimaryPart.Position,(lp.Character.PrimaryPart.CFrame*CFrame.new(0,0,-7.5)).Position)
  36.             local points = path:GetWaypoints()
  37.             for i=1,#points do
  38.                 local new = Instance.new("Part")
  39.                 new.Material = Enum.Material.Neon
  40.                 new.Transparency = 0.5
  41.                 new.Color = Color3.new(255,0,0)
  42.                 table.insert(parts,i,new)
  43.                 new.Anchored = true
  44.                 new.CanCollide = false
  45.                 new.Parent = workspace
  46.                 if i>1 then
  47.                     local mag = (points[i-1].Position-points[i].Position).Magnitude
  48.                     new.CFrame = CFrame.new(points[i-1].Position,points[i].Position)*CFrame.new(0,0,-mag/2)
  49.                     new.Size = Vector3.new(1,1,mag)
  50.                 end
  51.             end
  52.             if #points>0 then
  53.                 for i = 1,#points do
  54.                     local i = #points-(i-1)
  55.                     if health>0 and health2>0 then
  56.                         if path.Status == Enum.PathStatus.Success then
  57.                             lp.Character.Humanoid:MoveTo(points[i].Position)
  58.                             if points[i].Action == Enum.PathWaypointAction.Jump then
  59.                                 lp.Character.Humanoid.Jump = true
  60.                             end
  61.                             lp.Character.Humanoid.MoveToFinished:Wait()
  62.                             if i == 1 then
  63.                                 for i = 1,#parts do
  64.                                     parts[i]:Destroy()
  65.                                 end
  66.                                 parts = {}
  67.                                 db = true
  68.                             end
  69.                         else
  70.                             break
  71.                         end
  72.                     else
  73.                         break
  74.                     end
  75.                 end
  76.             end
  77.         end
  78.     else
  79.         if pos and not hum then
  80.             local parts = {}
  81.             local timer = tick()
  82.             local paths = game:GetService("PathfindingService")
  83.             local path = paths:CreatePath(params)
  84.             local health2 = lp.Character.Humanoid.Health
  85.             if health2>0 then
  86.                 path:ComputeAsync(pos,(lp.Character.PrimaryPart.CFrame*CFrame.new(0,0,-7.5)).Position)
  87.                 local points = path:GetWaypoints()
  88.                 for i=1,#points do
  89.                     local new = Instance.new("Part")
  90.                     new.Material = Enum.Material.Neon
  91.                     new.Transparency = 0.5
  92.                     new.BrickColor = BrickColor.new("Really red")
  93.                     table.insert(parts,i,new)
  94.                     new.Anchored = true
  95.                     new.CanCollide = false
  96.                     new.Parent = workspace
  97.                     if i>1 then
  98.                         local mag = (points[i-1].Position-points[i].Position).Magnitude
  99.                         new.CFrame = CFrame.new(points[i-1].Position,points[i].Position)*CFrame.new(0,0,-mag/2)
  100.                         new.Size = Vector3.new(0.5,0.5,mag)
  101.                     end
  102.                 end
  103.                 if #points>0 then
  104.                     for i = 1,#points do
  105.                         local i = #points-(i-1)
  106.                         if health2>0 then
  107.                             if path.Status == Enum.PathStatus.Success then
  108.                                 if tick()-timer<gotoRefresh then
  109.                                     local ray = Ray.new(lp.Character.PrimaryPart.Position,(lp.Character.PrimaryPart.Position-pos))
  110.                                     local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{lp.Character})
  111.                                     if hit then
  112.                                         lp.Character.Humanoid:MoveTo(points[i].Position)
  113.                                         if points[i].Action == Enum.PathWaypointAction.Jump then
  114.                                             lp.Character.Humanoid.Jump = true
  115.                                         end
  116.                                         lp.Character.Humanoid.MoveToFinished:Wait()
  117.                                         if i == 1 then
  118.                                             for i = 1,#parts do
  119.                                                 parts[i]:Destroy()
  120.                                             end
  121.                                             parts = {}
  122.                                         end
  123.                                     else
  124.                                         pf = false
  125.                                         break
  126.                                     end
  127.                                 else
  128.                                     print("timed out pathfinding")
  129.                                     for i = 1,#parts do
  130.                                         parts[i]:Destroy()
  131.                                     end
  132.                                     parts = {}
  133.                                     break
  134.                                 end
  135.                             else
  136.                                 break
  137.                             end
  138.                         else
  139.                             break
  140.                         end
  141.                     end
  142.                 end
  143.             end
  144.         end
  145.     end
  146. end
  147. function chat(string)
  148.     local Event = game:GetService("ReplicatedStorage").DefaultChatSystemChatEvents.SayMessageRequest
  149.     Event:FireServer(string)
  150. end
  151. function auto(dist)
  152.     while wait() do
  153.         local function FindTorso(dist)
  154.             local pos = lp.Character:FindFirstChild("HumanoidRootPart").Position
  155.             local list = game.Players:GetChildren()
  156.             local root = nil
  157.             local temp = nil
  158.             local human = nil
  159.             local temp2 = nil
  160.             for x = 1, #list do
  161.                 temp2 = list[x]
  162.                 if (temp2.className == "Player") and (temp2 ~= lp) then
  163.                     temp2 = temp2.Character
  164.                     temp = temp2:findFirstChild("HumanoidRootPart")
  165.                     human = temp2:findFirstChild("Humanoid")
  166.                     if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
  167.                         if (temp.Position - pos).magnitude < dist then
  168.                             root = temp
  169.                             dist = (temp.Position - pos).magnitude
  170.                         end
  171.                     end
  172.                 end
  173.             end
  174.             return root
  175.         end
  176.         if db == true then
  177.             local root = FindTorso(dist)
  178.             if root then
  179.                 local find = game.Players:FindFirstChild(root.Parent.Name)
  180.                 if find then
  181.                     local ray = Ray.new(lp.Character.PrimaryPart.Position,(lp.Character.PrimaryPart.Position-root.Position))
  182.                     local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{lp.Character})
  183.                     if not hit then
  184.                         target(find)
  185.                     else
  186.                         if hit.CanCollide == true then
  187.                             db = false
  188.                             Goto(find)
  189.                             repeat until db == true
  190.                             target(find)
  191.                         else
  192.                             target(find)
  193.                         end
  194.                     end
  195.                 end
  196.             end
  197.         else
  198.             break
  199.         end
  200.     end
  201. end
  202. function target(player)
  203.     if player~=nil then
  204.         local tool = nil
  205.         local hum = lp.Character:WaitForChild("Humanoid")
  206.         local hum2 = player.Character:WaitForChild("Humanoid")
  207.         local root1 = lp.Character:WaitForChild("HumanoidRootPart")
  208.         local root2 = player.Character:WaitForChild("HumanoidRootPart")
  209.         local chatted = false
  210.         while wait() do
  211.             repeat until hum ~=nil and hum2 ~=nil
  212.             local v1 = lp.Character
  213.             local v2 = lp.Character
  214.             local verify = player.Character:FindFirstChild("Humanoid").Health
  215.             local verify2 = lp.Character:FindFirstChild("Humanoid").Health
  216.             if v1 and v2 then
  217.                 if verify>0 then
  218.                     if verify2>0 then
  219.                         if db == true then
  220.                             local mag = (root1.Position-root2.Position).Magnitude
  221.                             if hum2.Jump==true then
  222.                                 hum.Jump = true
  223.                             end
  224.                             if mag<=15 then
  225.                                 local tools = lp.Backpack:GetChildren()
  226.                                 if #tools>0 then
  227.                                     tool = tools[1]
  228.                                     tool.Parent = lp.Character
  229.                                     tool:Activate()
  230.                                 else
  231.                                     if tool~=nil then
  232.                                         if tool.ClassName=="Tool" then
  233.                                             tool:Activate()
  234.                                         end
  235.                                     end
  236.                                 end
  237.                                 local frame = root2.CFrame*CFrame.new(-3,0,0)
  238.                                 local mag = (root1.Position-root2.Position).Magnitude
  239.                                 local ray = Ray.new(root1.Position,(root1.Position-player.Character.HumanoidRootPart.Position))
  240.                                 local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{lp.Character})
  241.                                 if not hit then
  242.                                     local frame = root2.CFrame*CFrame.new(-3,0,0)
  243.                                     local extraframe = root2.CFrame*CFrame.new(2,0,0)
  244.                                     cam.CFrame = CFrame.new(cam.CFrame.Position,extraframe.Position)
  245.                                     hum:MoveTo(frame.Position)
  246.                                 else
  247.                                     if hit then
  248.                                         local extraframe = root2.CFrame*CFrame.new(2,0,0)
  249.                                         cam.CFrame = CFrame.new(cam.CFrame.Position,extraframe.Position)
  250.                                         local frame = root2.CFrame*CFrame.new(-3,0,0)
  251.                                         print("hit "..hit.Name)
  252.                                         Goto(nil,frame.Position)--test
  253.                                     end
  254.                                 end
  255.                             else
  256.                                 local frame = root2.CFrame*CFrame.new(0,0,-7.5)
  257.                                 local extraframe = root2.CFrame*CFrame.new(-2,0,0)
  258.                                 local ray = Ray.new(lp.Character.PrimaryPart.Position,(lp.Character.PrimaryPart.Position-player.Character.PrimaryPart.Position))
  259.                                 local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{lp.Character})
  260.                                 cam.CFrame = CFrame.new(cam.CFrame.Position,extraframe.Position)
  261.                                 if hit then
  262.                                     print("hit")
  263.                                     Goto(nil,frame.Position)--test
  264.                                 else
  265.                                     if not hit then
  266.                                         hum:MoveTo(frame.Position)
  267.                                     end
  268.                                 end
  269.                             end
  270.                         else
  271.                             break
  272.                         end
  273.                     else
  274.                         availPlayers = {}
  275.                         if #currentOuts>0 then
  276.                             for i = 1,#currentOuts do
  277.                                 if currentOuts[i]~=nil then
  278.                                     currentOuts[i]:Destroy()
  279.                                 end
  280.                                 table.remove(currentOuts,i)
  281.                             end
  282.                         end
  283.                         for i = 1,#availPlayers do
  284.                             table.remove(availPlayers,i)
  285.                         end
  286.                         outlined=false
  287.                         break
  288.                     end
  289.                 else
  290.                     if chatted == false then chatted = true
  291.                         chat(player.Name.." is a noob")
  292.                     end
  293.                     if tool~=nil then
  294.                         tool.Parent = lp.Backpack
  295.                     end
  296.                     for i = 1,#currentOuts do
  297.                         if currentOuts[i].Parent.Parent == player.Character then
  298.                             table.remove(currentOuts,i)
  299.                         end
  300.                     end
  301.                     for i = 1,#availPlayers do
  302.                         if availPlayers[i]==player.Name then
  303.                             table.remove(availPlayers,i)
  304.                         end
  305.                     end
  306.                     break
  307.                 end
  308.             else
  309.                 break
  310.             end
  311.         end
  312.     end
  313. end
  314. function outlineP()
  315.     outlined = true
  316.     local players = game.Players:GetChildren()
  317.     for i = 1,#players do
  318.         local player = players[i]
  319.         local char = player.Character
  320.         local outline = Instance.new("SelectionBox")
  321.         outline.Color3 = Color3.new(255,0,0)
  322.         outline.LineThickness = .1
  323.         outline.SurfaceColor3 = Color3.new(172,0,0)
  324.         outline.SurfaceTransparency = .9
  325.         outline.Transparency = .3
  326.         local root = char:FindFirstChild("HumanoidRootPart")
  327.         if (root~=nil) then
  328.             outline.Parent = root
  329.             outline.Adornee = root
  330.             table.insert(availPlayers,i,player)
  331.             table.insert(currentOuts,i,outline)
  332.         else
  333.             outline:Destroy()
  334.         end
  335.     end
  336. end
  337. local mouse = lp:GetMouse()
  338. mouse.Button1Down:Connect(function()
  339.     local pos = mouse.Hit.p
  340.     local target = mouse.Target
  341.     if target then
  342.         if target.Parent:FindFirstChild("Humanoid")~=nil then
  343.             local find = game.Players:FindFirstChild(target.Parent.Name)
  344.             if (find~=nil) then
  345.                 if outlined == true then
  346.                     for o = 1,#availPlayers do
  347.                         if availPlayers[o].Name==find.Name then
  348.                             target(find)
  349.                             print("yes")
  350.                         end
  351.                     end
  352.                 else
  353.                     print("no outlined players")
  354.                 end
  355.             end
  356.         end
  357.     else
  358.         warn("no target selected")
  359.     end
  360. end)
  361. lp.Chatted:Connect(function(msg)
  362.     local text1 = ""
  363.     local text2 = ""
  364.     if string.lower(msg)=="/e outline" then
  365.         local players = game.Players:GetChildren()
  366.         if #currentOuts>0 then
  367.             for i = 1,#currentOuts do
  368.                 if currentOuts[i]~=nil then
  369.                     currentOuts[i]:Destroy()
  370.                 end
  371.                 table.remove(currentOuts,i)
  372.             end
  373.         end
  374.         if #availPlayers>0 then
  375.             for i = 1,#availPlayers do
  376.                 table.remove(availPlayers,i)
  377.             end
  378.         end
  379.         outlineP()
  380.         for i = 1,#players do
  381.             text1 = text1..players[i].Name..", "
  382.         end
  383.         local text2 = "[Autofight]: The player list is "..text1
  384.         --////////////
  385.         game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage",{
  386.             Text = text2..[[. The command for a specific player is "/e target PLAYER", you can use abbreviation as well.]];
  387.             Color = Color3.new(0,175,0);
  388.             Font = Enum.Font.SourceSansBold;
  389.             FontSize = Enum.FontSize.Size18;
  390.         })
  391.         --////////////
  392.     else
  393.         local msg = string.lower(msg)
  394.         if string.sub(msg,1,10)=="/e target " then
  395.             local players = game.Players:GetChildren()
  396.             if outlined == true then
  397.                 local name = string.lower(string.sub(msg,11,#msg))
  398.                 for i = 1,#players do
  399.                     local name2 = string.lower(players[i].Name)
  400.                     if string.sub(name2,1,#name)==name then
  401.                         for o = 1,#availPlayers do
  402.                             wait()
  403.                             if availPlayers[o].Name==players[i].Name then
  404.                                 local ray = Ray.new(lp.Character.PrimaryPart.Position,(lp.Character.PrimaryPart.Position-players[i].Character.PrimaryPart.Position))
  405.                                 local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{lp.Character})
  406.                                 if not hit then
  407.                                     game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage",{
  408.                                         Text = "[Autofight]: Now targeting "..players[i].Name..". make sure Shift Lock is on and dont move.";
  409.                                         Color = Color3.new(0,128,128);
  410.                                         Font = Enum.Font.SourceSansBold;
  411.                                         FontSize = Enum.FontSize.Size18;
  412.                                     })
  413.                                     db = true
  414.                                     target(players[i])
  415.                                 else
  416.                                     game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage",{
  417.                                         Text = "[Autofight]: Now targeting "..players[i].Name..". make sure Shift Lock is on and dont move.";
  418.                                         Color = Color3.new(0,128,128);
  419.                                         Font = Enum.Font.SourceSansBold;
  420.                                         FontSize = Enum.FontSize.Size18;
  421.                                     })
  422.                                     db = false
  423.                                     Goto(players[i])
  424.                                     repeat until db == true
  425.                                     target(players[i])
  426.                                 end
  427.                             end
  428.                         end
  429.                     end
  430.                 end
  431.             else
  432.                 game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage",{
  433.                     Text = "[Autofight]: No players outlined, use /e outline command to begin...";
  434.                     Color = Color3.new(255,0,0);
  435.                     Font = Enum.Font.SourceSansBold;
  436.                     FontSize = Enum.FontSize.Size18;
  437.                 })
  438.             end
  439.         else
  440.             if string.lower(msg) == "/e stop" then
  441.                 if db == true then db = false
  442.                 else
  443.                     print("Not targeting anyone")
  444.                                                                                                    print("AutoFight made by Indencito")
  445.                 end
  446.             else
  447.                 local msg = string.lower(msg)
  448.                 if string.sub(msg,1,7) == "/e auto" then
  449.                     if string.sub(msg,9,#msg)~=nil then
  450.                         local dist = tonumber(string.sub(msg,9,#msg))
  451.                         if db == true then db = false end
  452.                         db = true
  453.                         auto(dist)
  454.                     end
  455.                 end
  456.             end
  457.         end
  458.     end
  459. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement