Need help stopping a function loop

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? A spawning system

  2. What is the issue? It’s functional but triggers a function loop for some reason

  3. What solutions have you tried so far? looking up similar problems, to no result

 local function findspawn()


	if spawns[1] ~= nil then --SHOULD check if there's any avaliable spawns

		local selected = math.random(1, #spawns) --selects one of the avaliable spawns
		if spawns[selected]:GetChildren()[1] == nil then -- checks if it's free

			local me = spawns[selected]

			EventCosmetic()


			local sabo = game.ReplicatedStorage.Sabotuer:Clone()
			sabo.Parent = spawns[selected]
			sabo:MoveTo(spawns[selected].Position)
			sabo:PivotTo(spawns[selected].CFrame)
			spawns[selected].ChildRemoved:Connect(function()

				
				table.insert(spawns, me) -- adds itself back to the spawns list
				for _, i in pairs(spawns) do
					
					local num = #rawspawns -- check if all the spawns are empty by comparing the "spawns" table and the total amount of spawns and seeing if theyre the same
					if num == #spawns then
						
						if game.SoundService.SabotageTogether.Playing == true then
							
							game.SoundService.SabotageTogether.Playing = false --stop event and music if all are dead
							
							event.Value = "None" 
						end
						
					end
										
				
					
					
				end
					
					
					
				
				
				
					
					
			end)
			table.remove(spawns, selected)


		else

			findspawn()
		end
	else

		print("NOMORELEFT!")
	end
	
end

The sabotuer npcs get destroyed when interacted with and EventCosmetic just plays a sound, the findspawn() function gets played as many times as there are spawns avaliable in one of the events and just once in the other.

1 Like

For me to read it better. Give me some time.

 local function findspawn()
	if spawns[1] ~= nil then --SHOULD check if there's any avaliable spawns

		local selected = math.random(1, #spawns) --selects one of the avaliable spawns
		if spawns[selected]:GetChildren()[1] == nil then -- checks if it's free

			local me = spawns[selected]

			EventCosmetic()

			local sabo = game.ReplicatedStorage.Sabotuer:Clone()
			sabo.Parent = spawns[selected]
			sabo:MoveTo(spawns[selected].Position)
			sabo:PivotTo(spawns[selected].CFrame)

			spawns[selected].ChildRemoved:Connect(function()
				table.insert(spawns, me) -- adds itself back to the spawns list
				for _, i in pairs(spawns) do
					
					local num = #rawspawns -- check if all the spawns are empty by comparing the "spawns" table and the total amount of spawns and seeing if theyre the same
					if num == #spawns then
						
						if game.SoundService.SabotageTogether.Playing == true then
			
							game.SoundService.SabotageTogether.Playing = false --stop event and music if all are dead						
							event.Value = "None" 
						end		
					end	
				end
			end)

			table.remove(spawns, selected)
		else
			findspawn()
		end
	else
		print("NOMORELEFT!")
	end	
end

Please show me what your variable “spawns” is coming from. This will be your issue most likely.

If “spawns” is an array it’ll work; however, if not, your error is likely “spawns[selected]” due to the workspace trying to find a numbered spawn i.e “spawns[selected]” is similar to doing “spawns.1”

	local selected = math.random(1, #spawns)
-- type(selected) == "number"

if spawns[2]:GetChildren()[1] == nil then
-- spawns[2,3,4, etc.] is how it would work.
-- error "unable to find xyz" or "not a child or something like that"
-- In your case, it is going with else and looping the function