How do I make npcs spawn in a certain area?

How would I make it so that the npcs spawn in the script.parent (part).
The npcs torso spawns in the part but they keep spawning in 0,0,0 and not the part area. Im also trying to make it randomized in the area

image

npcs parents are in parts but not spawning on the part
image
image

Instead theyre spawning in here?
image

local function spawnEnemy(character)
	enemiesSpawned=enemiesSpawned+1
	local npc=character:Clone()
	local humanoid=npc.Humanoid
	local torso=npc.Torso
	npc.Parent=script.Parent
	npc:MakeJoints()
	local area = script.Parent
	local xVectorRandom = area.CFrame.RightVector * area.Size.X * (math.random(-50, 50) / 100)
	local yVectorRandom = area.CFrame.UpVector * area.Size.Y * (math.random(-50, 50) / 100)
	local zVectorRandom = area.CFrame.LookVector * area.Size.Z * (math.random(-50, 50) / 100)
	local randomarea = xVectorRandom + yVectorRandom + zVectorRandom
	torso.CFrame=CFrame.new(randomarea)
	humanoid.Died:connect(function()
		enemiesSpawned=enemiesSpawned-1
		wait(5)
		npc:Destroy()
	end)
	wait()
	humanoid.HealthChanged:connect(function()
		bloodify(torso)
	end)
	return npc
end

Is Torso the PrimaryPart? If not, set it so it is the PrimaryPart of the model.

yeah the torso is the primary part but its still not working

In the vectorRandoms, add the area.Position as well.

1 Like

yea it works now thank you very much :+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.