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