For loop only runs once

I have a for loop in a script I made, but it only runs once. The number of times the for loop runs should be equal to the number of things in a table which is used in the same script, and if I check how many items are in the table, it’s always the correct amount. No error is produced. What might the problem be?

Could you show your code? We can’t do anything without it.

What do you mean a for loop only runs once. However, it iterates based on what you specify in the for loop, is your problem iterating or running?

1 Like

I have no idea what you mean by that.

How many times are you expecting the code in your for loop to run

here is my code:

local areas = {its just 3 things inside of a table}

print(#areas)
	
for i,v in pairs(areas) do
	while wait (math.random(5,10)) do
		spawnEnemy(i)
	end
end

The code inside the for loop runs only once, despite the fact that the print just before the loop prints out the number 3. What might this mean? (In case you want to see the whole script, I’m just going to paste it below)

here is the entire script

local EnemyFolders = game.Workspace.EnemyFolders
local cave1Folder = EnemyFolders.Cave1Folder
local goblinKnightFolder = EnemyFolders.GoblinKnightFolder
local goblinKingFolder = EnemyFolders.GoblinKingFolder

local enemiesFolder = game.Lighting.Enemies
local goblin = enemiesFolder:FindFirstChild("Goblin")
local eliteGoblin = enemiesFolder:FindFirstChild("EliteGoblin")
local goblinKnight = enemiesFolder:FindFirstChild("GoblinKnight")
local goblinKing = enemiesFolder:FindFirstChild("GoblinKing")

local enemyFoldersList = {cave1Folder,goblinKnightFolder,goblinKingFolder}
local max = {5,2,1}
local cave1Enemies = {goblin,goblin,goblin,goblin,goblin,eliteGoblin}
local goblinKnightEnemies = {goblinKnight}
local goblinKingEnemies = {goblinKing}
local areas = {cave1Enemies,goblinKnightEnemies,goblinKingEnemies}

function spawnEnemy(areaNumber)
	print("enemy spawning attempt")
	local children = enemyFoldersList[areaNumber]:GetChildren()
	local area = areas[areaNumber]
	if #children < max[areaNumber] then
		print("enemy spawned")
		local selectedEnemy = area[math.random(#area)]
		print(selectedEnemy.Name)
		local enemyClone = selectedEnemy:Clone()
		local torso = enemyClone:FindFirstChild("Torso")
		if torso then
			enemyClone.Parent = enemyFoldersList[areaNumber]
		end
	end	
end

print(#areas)	
for	i,v in pairs(areas) do
	print(i) 
	while wait (math.random(5,10)) do
		spawnEnemy(i)
	end
end

areas is a table and does not have any children.

1 Like

A while loop will lock up the code, the code won’t run further until it is finished. I recommend reordering.

local areas = {its just 3 things inside of a table}

print(#areas)
	
while wait (math.random(5,10)) do
        for i,v in pairs(areas) do
		spawnEnemy(i)
	end
end

The problem now is that it will spawn someone everywhere at the same time.

local areas = {its just 3 things inside of a table}

print(#areas)
	
while true do
        for i,v in pairs(areas) do
		spawnEnemy(i)
        wait(math.random()*2)
	end
end

This last one will solve that problem. Change the *2 to however many seconds you want, between 0 and that number. Keep it smaller than you originally had.

3 Likes

Just implemented this and it works perfectly. Thanks so much!

1 Like