Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local myHuman = script.Parent:WaitForChild("Humanoid")
- local myTorso = script.Parent:WaitForChild("HumanoidRootPart")
- local rHand = script.Parent:WaitForChild("RightHand")
- local knife = script.Parent:WaitForChild("Knife")
- local knifeWeld = knife:WaitForChild("KnifeWeld")
- local bloodSpot = knife:WaitForChild("BloodSpot")
- local attackSound = knife:WaitForChild("Attack")
- local deadPlayerSound = knife:WaitForChild("DeadPlayer")
- local deadSound = knife:WaitForChild("DeadSound")
- local equipSound = knife:WaitForChild("Equip")
- local throwSound = knife:WaitForChild("Throw")
- local wallHitSound = knife:WaitForChild("WallHit")
- local downStab = script.Parent:WaitForChild("DownStab")
- local downStabAnim = myHuman:LoadAnimation(downStab)
- downStabAnim.Priority = Enum.AnimationPriority.Action
- local stabPunch = script.Parent:WaitForChild("StabPunch")
- local stabPunchAnim = myHuman:LoadAnimation(stabPunch)
- stabPunchAnim.Priority = Enum.AnimationPriority.Action
- local throw = script.Parent:WaitForChild("Throw")
- local throwAnim = myHuman:LoadAnimation(throw)
- throwAnim.Priority = Enum.AnimationPriority.Action
- local throwCharge = script.Parent:WaitForChild("ThrowCharge")
- local throwChargeAnim = myHuman:LoadAnimation(throwCharge)
- throwChargeAnim.Priority = Enum.AnimationPriority.Action
- local clone = script.Parent:Clone()
- local attackCool = true
- local pathFindFails = 0
- local charging = false
- function checkSight(target)
- local ray = Ray.new(myTorso.Position, (target.Position - myTorso.Position).Unit * 200)
- local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
- if hit then
- if hit:IsDescendantOf(target.Parent) then
- return true
- end
- end
- return false
- end
- function findTarget()
- local dist = 300
- local target = nil
- for i,v in ipairs(workspace:GetChildren()) do
- local human = v:FindFirstChild("Humanoid")
- local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
- if human and torso and v.Name ~= script.Parent.Name then
- if (myTorso.Position - torso.Position).magnitude < dist and human.Health > 0 then
- target = torso
- dist = (myTorso.Position - torso.Position).magnitude
- end
- end
- end
- return target
- end
- function pathToTarget(target)
- local path = game:GetService("PathfindingService"):CreatePath()
- path:ComputeAsync(myTorso.Position, target.Position)
- if path.Status == Enum.PathStatus.Success then
- pathFindFails = 0
- local waypoints = path:GetWaypoints()
- for i,v in ipairs(waypoints) do
- if v.Action == Enum.PathWaypointAction.Jump then
- myHuman.Jump = true
- end
- myHuman:MoveTo(v.Position)
- if checkSight(target) and math.abs(myTorso.Position.Y - target.Position.Y) < 1.5 then
- print("Direct line of sight to target and about the same height")
- break
- elseif (target.Position - waypoints[#waypoints].Position).magnitude > 30 then
- print("Target has moved away from the path")
- break
- elseif math.abs(myTorso.Position.Y - v.Position.Y) > 10 then
- print("I am too far from the waypoint heigtwise")
- break
- elseif (myTorso.Position - v.Position).magnitude > 10 then
- print("I have strayed too far from the path")
- break
- end
- if i % 5 ==0 then
- if findTarget() ~= target then
- print("Found better target")
- break
- end
- end
- local timeOut = myHuman.MoveToFinished:Wait()
- if not timeOut then
- getUnstuck()
- break
- end
- spawn(function()
- wait(1)
- if myTorso.Position.Y < myHuman.WalkToPoint.Y and
- (myTorso.Position - target.Position).magnitude > 10 and charging == false then
- print("The point is higher than my character we need to jump")
- myHuman.Jump = true
- end
- end)
- end
- else
- print("Failed to generate path...")
- pathFindFails = pathFindFails + 1
- if pathFindFails > 10 then
- getUnstuck()
- pathFindFails = 0
- end
- end
- end
- function squirtBlood(location)
- for i = 1, math.random(20,30) do
- local b = Instance.new("Part",script.Parent)
- b.CanCollide = false
- b.Shape = Enum.PartType.Ball
- b.Size = Vector3.new(math.random(5)/10,0,0)
- local bloodColors = {"Bright red","Really red","Crimson","Maroon"}
- b.BrickColor = BrickColor.new(bloodColors[math.random(#bloodColors)])
- b.Velocity = Vector3.new(math.random(-30,30),math.random(20,30),math.random(-30,30))
- b.CFrame = CFrame.new(location)
- game:GetService("Debris"):AddItem(b,1)
- end
- end
- function meleeAttack(target)
- if attackCool == true then
- myHuman:MoveTo(target.Position)
- attackSound:Play()
- local attack = math.random(4)
- if attack == 1 then
- stabPunchAnim:Play()
- else
- downStabAnim:Play()
- end
- deadSound:Play()
- target.Parent.Humanoid:TakeDamage(20)
- squirtBlood(bloodSpot.WorldPosition)
- if target.Parent.Humanoid.Health < 1 then
- deadPlayerSound:Play()
- wait(0.3)
- end
- attackCool = false
- spawn(function() wait(0.5) attackCool = true end)
- end
- end
- function rangeAttack(target)
- if attackCool == true then
- attackCool = false
- throwAnim:Play()
- wait(0.3)
- throwSound:Play()
- knifeWeld.Part1 = nil
- knife.Velocity = myTorso.CFrame.lookVector * 200
- knife.RotVelocity = Vector3.new(100,0,0)
- touched = knife.Touched:Connect(function(obj)
- if not obj:IsDescendantOf(script.Parent) and obj.Name ~= "Knife" then
- local clone = knife:Clone()
- clone.Parent = script.Parent
- clone.CFrame = knife.CFrame
- local w = Instance.new("WeldConstraint",clone)
- w.Part0 = clone
- w.Part1 = obj
- game:GetService("Debris"):AddItem(clone,10)
- knife.Velocity = Vector3.new(0,0,0)
- knife.CFrame = rHand.CFrame * CFrame.new(0,-0.5,-1) * CFrame.Angles(math.rad(90),math.rad(180),math.rad(180))
- knifeWeld.Part1 = rHand
- local human = obj.Parent:FindFirstChild("Humanoid")
- if human then
- human:TakeDamage(40)
- deadSound:Play()
- spawn(function()
- for i = 1, 10 do
- wait(0.2)
- squirtBlood(clone.BloodSpot.WorldPosition)
- end
- end)
- else
- wallHitSound:Play()
- end
- touched:Disconnect()
- end
- end)
- spawn(function() wait(1) attackCool = true end)
- end
- end
- function walkRandom()
- local xRand = math.random(-50,50)
- local zRand = math.random(-50,50)
- local goal = myTorso.Position + Vector3.new(xRand,0,zRand)
- local path = game:GetService("PathfindingService"):CreatePath()
- path:ComputeAsync(myTorso.Position, goal)
- if path.Status == Enum.PathStatus.Success then
- local waypoints = path:GetWaypoints()
- for i,v in ipairs(waypoints) do
- if v.Action == Enum.PathWaypointAction.Jump then
- myTorso.Jump = true
- end
- myHuman:MoveTo(v.Position)
- myHuman.MoveToFinished:Wait()
- end
- else
- print("Random path is fail")
- wait(2)
- end
- end
- function getUnstuck()
- print("My humanoid is stuck, attempting to get unstuck")
- myHuman:Move(Vector3.new(math.random(-1,1),0,math.random(-1,1)))
- wait(1)
- end
- function chase(target)
- charging = true
- myHuman:MoveTo(target.Position)
- end
- myHuman.Died:Connect(function()
- wait(15)
- clone.Parent = workspace
- game:GetService("Debris"):AddItem(script.Parent,0.1)
- end)
- function main()
- charging = false
- local target = findTarget()
- if target then
- if checkSight(target) and math.abs(myTorso.Position.Y - target.Position.Y) < 1.5 or
- checkSight(target) and (myTorso.Position - target.Position).magnitude < 3 then
- local dist = (myTorso.Position - target.Position).magnitude
- if dist > 3 then
- myHuman.WalkSpeed = 20
- chase(target)
- end
- if dist < 5 then
- meleeAttack(target)
- elseif dist < 50 and dist > 20 then
- rangeAttack(target)
- end
- else
- print("Main function we have to pathfind")
- myHuman.WalkSpeed = 10
- pathToTarget(target)
- end
- else
- walkRandom()
- end
- end
- while wait(0.1) do
- if myHuman.Health < 1 then
- break
- end
- main()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement