Please help! Spawn issue!

Hey guys. I realized my last topic was a bit hard to read, so I remade it. Let’s get to the point yeah? Alright, so basically, I made a script that clones my NPCs. Let’s say I want clone number 1 to be teleported to this position: Vector3.new(1, 1, 1).

Okay, so that’s done. Now what about clone number 2?
I want their position to be: Vector3.new(-5, 10, 4). Now for the final NPC.

Their position should be: Vector3.new(10, 10, 10). Alright, done! Except, I’m not. This is all done in a for loop, and I have no idea of how to make certain NPCs spawn at a certain point. If you can help me, that’d be greatly appreciated…

Code:

for i = 1, 3 do
		local EnemyModel = Enemy:Clone()
		EnemyModel.Parent = EnemyFolder
		EnemyModel:PivotTo(CFrame.new(Enemy.WorldPivot.Position+Vector3.new(math.random(-50, 50), 0, math.random(-50, 50))))
		if EnemyModel and Humanoid and Humanoid.Health > 0 then
			EnemyFolder.Enemies.Value += 1
		end
	end
2 Likes

math.random() makes their positions random. So, obviously I can’t use that.

1 Like

Instead of using a for loop like that, you can create a table that stores positions for each NPC you want. Then loop through the table and spawn each enemy to the position stored in the table.

Example:

local enemiesTable = {
	{
		Position = Vector3.new(1, 1, 1) -- You can also enter other data such as name, etc. If you want to extend this system. (Just a suggestion though)
	},
	{
		Position = Vector3.new(-5, 10, 4) 
	},
	{
		Position = Vector3.new(10, 10, 10) 
	}
}

for _, enemyData in enemiesTable do
	local EnemyModel: Model = Enemy:Clone()
	EnemyModel.Parent = EnemyFolder
	EnemyModel:MoveTo(enemyData.Position)
	
	if EnemyModel and Humanoid and Humanoid.Health > 0 then
		EnemyFolder.Enemies.Value += 1
	end
end

Wow, this is actually so smart! I can’t believe I never thought to do this. Thank you so much for this!

1 Like

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