local humanoid = script.Parent:WaitForChild("Humanoid") local animationController = humanoid:WaitForChild("AnimationController") local monster = game.Workspace.Monster local player = nil local sight = script.Parent:WaitForChild("Sight") local chaseRange = 10 -- Adjust the chase range as needed local idleAnimation = animationController:LoadAnimation(game.Workspace.Animations.Monster.Idle) local walkAnimation = animationController:LoadAnimation(game.Workspace.Animations.Monster.Walk) local waypointReachedDistance = 2 local patrolWaypoints = { Vector3.new(10, 0, 0), Vector3.new(0, 0, 10), Vector3.new(-10, 0, 0), Vector3.new(0, 0, -10), } -- Define waypoints for the monster to patrol local lineOfSightRange = 20 local function StartChase() walkAnimation:Play() end local function StopChase() walkAnimation:Stop() idleAnimation:Play() end local function CheckPlayerInSight() local direction = (player.Character.HumanoidRootPart.Position - monster.Position).Magnitude if direction <= chaseRange then -- Check if there is a clear line of sight to the player local raycastParams = RaycastParams.new() raycastParams.FilterType = Enum.RaycastFilterType.Blacklist raycastParams.FilterDescendantsInstances = {monster} local result = workspace:Raycast(monster.Position, player.Character.HumanoidRootPart.Position - monster.Position, raycastParams) if result and result.Instance:IsDescendantOf(player.Character) then StartChase() else StopChase() end else StopChase() end end local function OnTouched(part) if part.Parent:FindFirstChild("Humanoid") then player = part.Parent CheckPlayerInSight() end end sight.Touched:Connect(OnTouched) while true do wait(0.1) if player then local direction = player.Character.HumanoidRootPart.Position - monster.Position local distanceToPlayer = direction.Magnitude if distanceToPlayer > chaseRange then StopChase() player = nil else -- Chase the player humanoid:MoveTo(player.Character.HumanoidRootPart.Position) end else if #patrolWaypoints > 0 then local currentWaypointIndex = 1 local currentWaypoint = patrolWaypoints[currentWaypointIndex] local distanceToWaypoint = (currentWaypoint - monster.Position).Magnitude if distanceToWaypoint <= waypointReachedDistance then currentWaypointIndex = currentWaypointIndex + 1 if currentWaypointIndex > #patrolWaypoints then currentWaypointIndex = 1 end currentWaypoint = patrolWaypoints[currentWaypointIndex] end humanoid:MoveTo(currentWaypoint) else -- Idle behavior StopChase() end end end