local PathfindingService = game:GetService("PathfindingService")
local mob = script.Parent
local Humanoid = mob.Humanoid
mob.PrimaryPart:SetNetworkOwner(nil)
local function canSeeTarget(target)
local origin = mob.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - mob.HumanoidRootPart.Position).Unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, mob)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 40
local nearestTarget
for i, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (mob.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
mob.HumanoidRootPart.ISEEYOU:Play()
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {
["AgentHight"] = 12,
["AgentRadious"] = 10,
["AgentCanJump"] = false,
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(mob.HumanoidRootPart.Position, destination.Position)
return path
end