How would I make NPC's wander around their area and spawn/despawn them whenever a player enters or exits their territory?

Heya there.

I’m making a basic NPC system where if you enter an area, specific types of enemies will spawn. Each area will have a walkpoint which enemies will be able to wander around. I’m wondering, however, how am I able to achieve this and how do I go making enemies spawn/despawn whenever a player enters or exits the area?

(The code is still fairly new)

--[[SERVICES]]--
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

--[[MODULE]]--
local BaseEntityModule = {}
BaseEntityModule.__index = BaseEntityModule

--//Creating the entity and positioning it
function BaseEntityModule:CreateEntity(EntityName)
	for _, Entity in pairs(ServerStorage.Entities:GetChildren()) do
		if Entity:IsA("Model") and Entity.Name == EntityName then
			
			--//Creating the entity
			local self = setmetatable({}, BaseEntityModule)
			
			self.Unit = Entity:Clone()
			self.Humanoid = self.Unit:FindFirstChildWhichIsA("Humanoid")
			self:PlaceEntity(workspace.SpawnZone1)
			
			return self
			
		else
			warn("Entity doesn't exist:"..EntityName)
			return
		end
	end
end

--This might've not been necessary to implement(?)
function BaseEntityModule:PlaceEntity(SpawnZone)
	local WalkPoints = SpawnZone:GetChildren()
	local SelectionPoint = WalkPoints[math.random(1,#WalkPoints)]
	
	--//Spawning the entity onto the workspace and placing it in a walkpoint.
	self.Unit.Parent = game.Workspace
	self.Unit:PivotTo(SelectionPoint:GetPivot())
end

--//Chasing & Pathfidning
function BaseEntityModule:WalkToRandomPoint()
	--//TBA
end

return BaseEntityModule

There won’t be any code examples cause I’m gonna assume you know how to script these :d if not I’ll see what I can do I guess

First make the Area and when a something touches the area check if it’s a player and start the script.

Then make a while loop that checks if the player is in or has left the area using workspace:GetPartsInParts() while also making use of params so you can include only parts of a player.

I’m going to assume you have the NPC being able to walk to places done cause explaining that would be a bit hard to do. If not well Ill see what I can do.

To make the NPC’s walk in a random place within an area you can take the Area that is detecting the player (or a custom area from when you start the script) and pick a random position within the area then make a Raycast downward to detect the floor for where the NPC is walking to. I’d also start the Raycast like 50 studs up or something depending on if the area is a mix between high and low parts of a map. There is a problem with this though, the NPC might pick a position thats far away. To combat this,

Make another area that takes the position of the NPC and set the new size of the new area to be like 50,50,50 or however big you want it. Then make the NPC pick a position in the NewArea to walk to while also checking if the position to walk to (not new area) is within the Area it can spawn in, if not, pick another position.

Hopefully I explained this well, its currently 12:30, I need to sleep