Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PathfindingService = game:GetService("PathfindingService")
- local enemies = game:GetService("ServerStorage").Enemies:GetChildren()
- local Enemy = {}
- Enemy.__index = Enemy
- function Enemy.new(spawnPoint)
- local self = setmetatable({}, Enemy)
- local enemy = Enemy:Spawn(spawnPoint)
- self.Enemy = enemy
- self.Humanoid = enemy:WaitForChild("Humanoid")
- self.Root = enemy:WaitForChild("HumanoidRootPart")
- self.Root:SetNetworkOwner(enemy)
- self.Health = enemy:GetAttribute("Health")
- self.Humanoid.MaxHealth = self.Health
- self.Damage = enemy:GetAttribute("Damage")
- self.Target = nil
- return self
- end
- function Enemy:Init()
- while wait(0.1) do
- if self.Humanoid.Health < 1 then
- break
- end
- self:FindTarget()
- if self.Target then
- self.Humanoid.WalkSpeed = 16
- self:FindPath()
- else
- self.Humanoid.WalkSpeed = 8
- self:Wander()
- end
- end
- end
- function Enemy:Spawn(spawnPoint)
- local enemy = enemies[math.random(1, #enemies)]:Clone()
- enemy.Parent = workspace
- enemy:SetPrimaryPartCFrame(spawnPoint.CFrame)
- return enemy
- end
- function Enemy:Wander()
- local xRand = math.random(-50, 50)
- local zRand = math.random(-50, 50)
- local goal = self.Root.Position + Vector3.new(xRand, 0, zRand)
- local path = PathfindingService:CreatePath()
- path:ComputeAsync(self.Root.Position, goal)
- local waypoints = path:GetWaypoints()
- if path.Status == Enum.PathStatus.Success then
- for _, waypoint in ipairs(waypoints) do
- if waypoint.Action == Enum.PathWaypointAction.Jump then
- self.Humanoid.Jump = true
- end
- self.Humanoid:MoveTo(waypoint.Position)
- local timeOut = self.Humanoid.MoveToFinished:Wait(1)
- if not timeOut then
- print("Enemy Stuck")
- self.Humanoid.Jump = true
- self:Wander()
- end
- end
- else
- print("Path failed")
- self:Wander()
- end
- end
- function Enemy:CheckSight()
- if self.Target then
- local ray = Ray.new(self.Root.Position, (self.Target.Position - self.Root.Position).Unit * 40)
- local hit, position = workspace:FindPartOnRayWithIgnoreList(ray, {self.Enemy})
- if hit then
- if hit:IsDescendantOf(self.Target) and math.abs(hit.Position.Y - self.Root.Position.Y) < 3 then
- print("Enemy can see the target")
- return true
- end
- end
- return false
- else
- return false
- end
- end
- function Enemy:FindTarget()
- local distance = 200
- local potentialTargets = {}
- local seeTargets = {}
- 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 ~= self.Enemy.Name then
- if (self.Root.Position - torso.Position).magnitude < distance and human.Health > 0 then
- table.insert(potentialTargets, torso)
- end
- end
- end
- if #potentialTargets > 0 then
- for i, v in ipairs(potentialTargets) do
- if self:CheckSight() then
- table.insert(seeTargets, v)
- elseif #seeTargets == 0 and (self.Root.Position - v.Position).magnitude < distance then
- self.Target = v
- distance = (self.Root.Position - v.Position).magnitude
- end
- end
- end
- if #seeTargets > 0 then
- distance = 200
- for i, v in ipairs(seeTargets) do
- if (self.Root.Position - v.Position).magnitude < distance then
- self.Target = v
- distance = (self.Root.Position - v.Position).magnitude
- end
- end
- end
- end
- function Enemy:FindPath()
- local path = PathfindingService:CreatePath()
- path:ComputeAsync(self.Root.Position, self.Target.Position)
- local waypoints = path:GetWaypoints()
- if path.Status == Enum.PathStatus.Success then
- for _, waypoint in ipairs(waypoints) do
- if waypoint.Action == Enum.PathWaypointAction.Jump then
- self.Humanoid.Jump = true
- end
- self.Humanoid:MoveTo(waypoint.Position)
- local timeOut = self.Humanoid.MoveToFinished:Wait(1)
- if not timeOut then
- self.Humanoid.Jump = true
- self:FindPath()
- break
- end
- if self:CheckSight() then
- repeat
- print("Moving to target")
- self.Humanoid:MoveTo(self.Target.Position)
- wait(0.1)
- if self.Target == nil then
- break
- elseif self.Target.Parent == nil then
- break
- end
- until self:CheckSight() == false or self.Humanoid.Health < 1 or self.Target.Parent.Humanoid.Health < 1
- break
- end
- if (self.Root.Position - waypoints[1].Position).magnitude > 20 then
- print("Target has moved, generating new path")
- self:FindPath()
- break
- end
- end
- end
- end
- return Enemy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement