chromeJ

Y3M Scream Pathfinding

Jan 11th, 2021
934
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local myHuman = script.Parent:WaitForChild("Humanoid")
  2. local myTorso = script.Parent:WaitForChild("HumanoidRootPart")
  3. local rHand = script.Parent:WaitForChild("RightHand")
  4.  
  5. local knife = script.Parent:WaitForChild("Knife")
  6. local knifeWeld = knife:WaitForChild("KnifeWeld")
  7. local bloodSpot = knife:WaitForChild("BloodSpot")
  8.  
  9. local attackSound = knife:WaitForChild("Attack")
  10. local deadPlayerSound = knife:WaitForChild("DeadPlayer")
  11. local deadSound = knife:WaitForChild("DeadSound")
  12. local equipSound = knife:WaitForChild("Equip")
  13. local throwSound = knife:WaitForChild("Throw")
  14. local wallHitSound = knife:WaitForChild("WallHit")
  15.  
  16. local downStab = script.Parent:WaitForChild("DownStab")
  17. local downStabAnim = myHuman:LoadAnimation(downStab)
  18. downStabAnim.Priority = Enum.AnimationPriority.Action
  19.  
  20. local stabPunch = script.Parent:WaitForChild("StabPunch")
  21. local stabPunchAnim = myHuman:LoadAnimation(stabPunch)
  22. stabPunchAnim.Priority = Enum.AnimationPriority.Action
  23.  
  24. local throw = script.Parent:WaitForChild("Throw")
  25. local throwAnim = myHuman:LoadAnimation(throw)
  26. throwAnim.Priority = Enum.AnimationPriority.Action
  27.  
  28. local throwCharge = script.Parent:WaitForChild("ThrowCharge")
  29. local throwChargeAnim = myHuman:LoadAnimation(throwCharge)
  30. throwChargeAnim.Priority = Enum.AnimationPriority.Action
  31.  
  32. local clone = script.Parent:Clone()
  33. local attackCool = true
  34. local pathFindFails = 0
  35. local charging = false
  36.  
  37. function checkSight(target)
  38.     local ray = Ray.new(myTorso.Position, (target.Position - myTorso.Position).Unit * 200)
  39.     local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
  40.     if hit then
  41.         if hit:IsDescendantOf(target.Parent) then
  42.             return true
  43.         end
  44.     end
  45.     return false
  46. end
  47.  
  48. function findTarget()
  49.     local dist = 300
  50.     local target = nil
  51.     for i,v in ipairs(workspace:GetChildren()) do
  52.         local human = v:FindFirstChild("Humanoid")
  53.         local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
  54.         if human and torso and v.Name ~= script.Parent.Name then
  55.             if (myTorso.Position - torso.Position).magnitude < dist and human.Health > 0 then
  56.                 target = torso
  57.                 dist = (myTorso.Position - torso.Position).magnitude
  58.             end
  59.         end
  60.     end
  61.     return target
  62. end
  63.  
  64. function pathToTarget(target)
  65.     local path = game:GetService("PathfindingService"):CreatePath()
  66.     path:ComputeAsync(myTorso.Position, target.Position)
  67.     if path.Status == Enum.PathStatus.Success then
  68.         pathFindFails = 0
  69.         local waypoints = path:GetWaypoints()
  70.         for i,v in ipairs(waypoints) do
  71.             if v.Action == Enum.PathWaypointAction.Jump then
  72.                 myHuman.Jump = true
  73.             end
  74.             myHuman:MoveTo(v.Position)
  75.             if checkSight(target) and math.abs(myTorso.Position.Y - target.Position.Y) < 1.5 then
  76.                 print("Direct line of sight to target and about the same height")
  77.                 break
  78.             elseif (target.Position - waypoints[#waypoints].Position).magnitude > 30 then
  79.                 print("Target has moved away from the path")
  80.                 break
  81.             elseif math.abs(myTorso.Position.Y - v.Position.Y) > 10 then
  82.                 print("I am too far from the waypoint heigtwise")
  83.                 break
  84.             elseif (myTorso.Position - v.Position).magnitude > 10 then
  85.                 print("I have strayed too far from the path")
  86.                 break
  87.             end
  88.             if i % 5 ==0 then
  89.                 if findTarget() ~= target then
  90.                     print("Found better target")
  91.                     break
  92.                 end
  93.             end
  94.             local timeOut = myHuman.MoveToFinished:Wait()
  95.             if not timeOut then
  96.                 getUnstuck()
  97.                 break
  98.             end
  99.             spawn(function()
  100.                 wait(1)
  101.                 if myTorso.Position.Y < myHuman.WalkToPoint.Y and
  102.                     (myTorso.Position - target.Position).magnitude > 10 and charging == false then
  103.                     print("The point is higher than my character we need to jump")
  104.                     myHuman.Jump = true
  105.                 end
  106.             end)
  107.         end
  108.     else
  109.         print("Failed to generate path...")
  110.         pathFindFails = pathFindFails + 1
  111.         if pathFindFails > 10 then
  112.             getUnstuck()
  113.             pathFindFails = 0
  114.         end
  115.     end
  116. end
  117.  
  118. function squirtBlood(location)
  119.     for i = 1, math.random(20,30) do
  120.         local b = Instance.new("Part",script.Parent)
  121.         b.CanCollide = false
  122.         b.Shape = Enum.PartType.Ball
  123.         b.Size = Vector3.new(math.random(5)/10,0,0)
  124.         local bloodColors = {"Bright red","Really red","Crimson","Maroon"}
  125.         b.BrickColor = BrickColor.new(bloodColors[math.random(#bloodColors)])
  126.         b.Velocity = Vector3.new(math.random(-30,30),math.random(20,30),math.random(-30,30))
  127.         b.CFrame = CFrame.new(location)
  128.         game:GetService("Debris"):AddItem(b,1)
  129.     end
  130. end
  131.  
  132. function meleeAttack(target)
  133.     if attackCool == true then
  134.         myHuman:MoveTo(target.Position)
  135.         attackSound:Play()
  136.         local attack = math.random(4)
  137.         if attack == 1 then
  138.             stabPunchAnim:Play()
  139.         else
  140.             downStabAnim:Play()
  141.         end
  142.         deadSound:Play()
  143.         target.Parent.Humanoid:TakeDamage(20)
  144.         squirtBlood(bloodSpot.WorldPosition)
  145.         if target.Parent.Humanoid.Health < 1 then
  146.             deadPlayerSound:Play()
  147.             wait(0.3)
  148.         end
  149.         attackCool = false
  150.         spawn(function() wait(0.5) attackCool = true end)
  151.     end
  152. end
  153.  
  154. function rangeAttack(target)
  155.     if attackCool == true then
  156.         attackCool = false
  157.         throwAnim:Play()
  158.         wait(0.3)
  159.         throwSound:Play()
  160.         knifeWeld.Part1 = nil
  161.         knife.Velocity = myTorso.CFrame.lookVector * 200
  162.         knife.RotVelocity = Vector3.new(100,0,0)
  163.         touched = knife.Touched:Connect(function(obj)
  164.             if not obj:IsDescendantOf(script.Parent) and obj.Name ~= "Knife" then
  165.                 local clone = knife:Clone()
  166.                 clone.Parent = script.Parent
  167.                 clone.CFrame = knife.CFrame
  168.                 local w = Instance.new("WeldConstraint",clone)
  169.                 w.Part0 = clone
  170.                 w.Part1 = obj
  171.                 game:GetService("Debris"):AddItem(clone,10)
  172.                 knife.Velocity = Vector3.new(0,0,0)
  173.                 knife.CFrame = rHand.CFrame * CFrame.new(0,-0.5,-1) * CFrame.Angles(math.rad(90),math.rad(180),math.rad(180))
  174.                 knifeWeld.Part1 = rHand
  175.                
  176.                 local human = obj.Parent:FindFirstChild("Humanoid")
  177.                 if human then
  178.                     human:TakeDamage(40)
  179.                     deadSound:Play()
  180.                     spawn(function()
  181.                         for i = 1, 10 do
  182.                             wait(0.2)
  183.                             squirtBlood(clone.BloodSpot.WorldPosition)
  184.                         end
  185.                     end)
  186.                 else
  187.                     wallHitSound:Play()
  188.                 end
  189.                 touched:Disconnect()
  190.             end
  191.         end)
  192.         spawn(function() wait(1) attackCool = true end)
  193.     end
  194. end
  195.  
  196. function walkRandom()
  197.     local xRand = math.random(-50,50)
  198.     local zRand = math.random(-50,50)
  199.     local goal = myTorso.Position + Vector3.new(xRand,0,zRand)
  200.    
  201.     local path = game:GetService("PathfindingService"):CreatePath()
  202.     path:ComputeAsync(myTorso.Position, goal)
  203.    
  204.     if path.Status == Enum.PathStatus.Success then
  205.         local waypoints = path:GetWaypoints()
  206.         for i,v in ipairs(waypoints) do
  207.             if v.Action == Enum.PathWaypointAction.Jump then
  208.                 myTorso.Jump = true
  209.             end
  210.             myHuman:MoveTo(v.Position)
  211.             myHuman.MoveToFinished:Wait()
  212.         end
  213.     else
  214.         print("Random path is fail")
  215.         wait(2)
  216.     end
  217. end
  218.  
  219. function getUnstuck()
  220.     print("My humanoid is stuck, attempting to get unstuck")
  221.     myHuman:Move(Vector3.new(math.random(-1,1),0,math.random(-1,1)))
  222.     wait(1)
  223. end
  224.  
  225. function chase(target)
  226.     charging = true
  227.     myHuman:MoveTo(target.Position)
  228. end
  229.  
  230. myHuman.Died:Connect(function()
  231.     wait(15)
  232.     clone.Parent = workspace
  233.     game:GetService("Debris"):AddItem(script.Parent,0.1)
  234. end)
  235.  
  236. function main()
  237.     charging = false
  238.     local target = findTarget()
  239.     if target then
  240.         if checkSight(target) and math.abs(myTorso.Position.Y - target.Position.Y) < 1.5 or
  241.             checkSight(target) and (myTorso.Position - target.Position).magnitude < 3 then
  242.             local dist = (myTorso.Position - target.Position).magnitude
  243.             if dist > 3 then
  244.                 myHuman.WalkSpeed = 20
  245.                 chase(target)
  246.             end
  247.            
  248.             if dist < 5 then
  249.                 meleeAttack(target)
  250.             elseif dist < 50 and dist > 20 then
  251.                 rangeAttack(target)
  252.             end
  253.         else
  254.             print("Main function we have to pathfind")
  255.             myHuman.WalkSpeed = 10
  256.             pathToTarget(target)
  257.         end    
  258.     else
  259.         walkRandom()
  260.     end
  261. end
  262.  
  263. while wait(0.1) do
  264.     if myHuman.Health < 1 then
  265.         break
  266.     end
  267.     main()
  268. end
RAW Paste Data