For Loop is Skipping Every Iteration Except for Last

local alivePlayers = { }
local spawnPoints = { }
local connection
local giveDelay = 2

function look(player)
	for i,v in pairs(alivePlayers) do 
		if player.UserId == v.UserId then
			return true
		end
	end
	return false
end

function printAllSpawns()
	for i,v in ipairs(spawnPoints) do 
		print(v.Name, i)
	end
end

function findOpenSpawn(player)
	for i,v in ipairs(spawnPoints) do 
		local doorSettings = v:FindFirstChild("SpawnSettings")
		if doorSettings then 
			local Lastused = doorSettings:FindFirstChild("LastUsed")
			local Cooldown = doorSettings:FindFirstChild("Cooldown")
			if Lastused.Value ~= 0 then
				print(Lastused.Value)
				print(Cooldown.Value)
				if Lastused.Value + Cooldown.Value <= os.time() then
					print("spawning "..player.Name)
					Lastused.Value = os.time()
					print(Lastused.Value + Cooldown.Value)
					print(os.time())
					print(v.Name)
					table.insert(alivePlayers, player)
					player.Character:SetPrimaryPartCFrame(v.CFrame)
					game:GetService("ReplicatedStorage").SpawnPlayer:FireClient(player)
					return true
				else 
					return false
				end
			else 
				print("0")
				print("spawning "..player.Name)
				Lastused.Value = os.time()
				table.insert(alivePlayers, player)
				player.Character:SetPrimaryPartCFrame(v.CFrame)
				game:GetService("ReplicatedStorage").SpawnPlayer:FireClient(player)
				return true
			end
		end
	end
end

for i,v in pairs(game.Workspace:GetChildren()) do 
	if v:FindFirstChild("SpawnSettings") then 
		print(v.Name, i)
		table.insert(spawnPoints, v)
		v.Transparency = 1
	end
end

while wait(2) do 
	for i,v in pairs(game:GetService("Players"):GetPlayers()) do 
		if not look(v) then
			local success = findOpenSpawn(v)
			if success then
				print("Successful.")
				local char = v.Character
				local humanoid = char:FindFirstChild("Humanoid")
				if not humanoid then
					while not humanoid do 
						wait(.1)
						humanoid = char:FindFirstChild("Humanoid")
						if humanoid then
							break
						end
					end
				end
				print("Connection established.")
				connection = humanoid.Died:Connect(function()
					wait(game.Players.RespawnTime)
					table.remove(alivePlayers, i)
					print("removed "..v.Name)
					connection:Disconnect()
				end)
			end
		end
	end
end

At the current moment, it appears to only be trying to spawn everyone at one spawn point and skips over all the rest. I believe the issue is with the for loop, but I’m not sure.

1 Like

I think it’s because you have return and break statements scattered throughout your code.

I don’t believe it’s skipping through the rest of the spawn points. By any chance, is the “one spawn point,” the first spawn point in spawnPoints? ipairs loops through the array in order. That means if the first spawn point is ready to use, it will teleport the player. If it is not ready to use, it returns false, saying their is no open spawn point, ending the loop and the function. You check again, and it will be the same spawn point. All you have to do it make the second if-else statement in the for loop just an if statement. Dont return false. Hope this makes sense because I am really bad at explaining this. (although this is a pretty cool way to make a cooldown)

Honestly, not sure how I didn’t see that the first time. Thanks.