I have made a system that will spawn npc’s at a random position within a part, however npc’s have been spawning outside of the part. Although this happens on one of the 5 parts.
--Functions
local function GetRandomPos(position, xSize, zSize, ySize)
local randomX = math.random(-xSize/2, xSize/2)
local randomZ = math.random(-zSize/2 , zSize/2)
local spawnAt = Vector3.new(randomX, ySize, randomZ)
return spawnAt + position
end
local function SpawnNpc(stage)
task.wait(0.65)
--Npc
local clone = npc:Clone()
local hum = clone.Humanoid
--Animations & Prompt
local idleAnim = npc.Animation.Idle
local prompt = clone.HumanoidRootPart.GiveJoke
prompt.HoldDuration = player.Stages[stage.Configuration.StageName.Value].JokeTime.Value
--Spawn
clone.PrimaryPart.Position = GetRandomPos(stage.Position, stage.Size.X, stage.Size.Z, stage.Size.Y + 2.5)
local npcAngleOffset = CFrame.Angles(0, math.rad(math.random(-360, 360)),0)
clone:SetPrimaryPartCFrame(clone:GetPrimaryPartCFrame() * npcAngleOffset)
clone.Parent = stage
This script gets the size and position of a part then returns a vector 3 for NPC’s to spawn within.
As shown here, none of the NPC’s are spawning outside of their part
Although on this one NPC’s are…
Im not sure why this is happening and I have gotten no where.
Here are all the parts in explorer if that helps
Some are also spawning within buildings, but that has to do with them not spawning in their set boundaries.