Fixed the issue.
Change the NPCSpawner.spawnNPCs function inside the Module to this:
function NPCSpawner.spawnNPCs(npcModel, spawnLocation)
if spawnLocation.Name == "SpawnLocation" then
local existingNPC = getNPCAtLocation(spawnLocation)
local spawnCFrame
if existingNPC then
-- Spawn on top of existing NPC
spawnCFrame = existingNPC.PrimaryPart.CFrame + Vector3.new(0, existingNPC.PrimaryPart.Size.Y * 2, 0)
else
-- Spawn at a random position within the size of the spawnLocation
local randomPosition = getRandomPositionInSpawnLocation(spawnLocation)
spawnCFrame = CFrame.new(randomPosition) + Vector3.new(0, spawnLocation.Size.Y * 2, 0)
end
spawnNPC(npcModel, spawnCFrame)
else
warn("Invalid name, expected SpawnLocation")
end
end
Script
local NPCSpawner = require(game:GetService("ServerScriptService").NPCSpawnerModule)
local npcModel = game.ServerStorage.NPCModel -- Replace with your NPC model or change this to a for loop.
-- Find all SpawnLocation objects in workspace
local spawnLocations = {}
for _, descendant in pairs(workspace:GetDescendants()) do
if descendant.Name == "SpawnLocation" then
table.insert(spawnLocations, descendant)
end
end
if #spawnLocations > 0 then
NPCSpawner.spawnNPCs(npcModel, spawnLocations[math.random(1, #spawnLocations)])
end