For i pairs loop stops randomly

So. I had this generation for a while and it kept stopping.
This is the code.

function Gen(Room,RoomTable,Finished)
	Finished = false
	repeat wait()
		print("1a")
		print("Restart")
		if Finished == false then
			wait()
			print("2a")
			Finished = false
			local Count = 0
			RoomTable = {}
			local RandomLine = game.Workspace.Map:GetChildren() --Get Random Position
			local i = RandomLine[math.random(1, #RandomLine)] --Get Random Line
			wait()
			if i.Special.Value == false then
				print("3a")
				for _,a in pairs(Room:GetChildren()) do
					print("4a")
					a.Finished.Value = false
					if game.Workspace.Map:FindFirstChild("" .. convert2(i.Letter.Value + a.Ltr.Value) .. "-" ..tonumber(i.NumberText.SurfaceGui.TextLabel.Text)+ a.Num.Value) then --Get the Line where the room should be
						print("5a")
						if game.Workspace.Map:FindFirstChild("" .. convert2(i.Letter.Value + a.Ltr.Value) .. "-" ..tonumber(i.NumberText.SurfaceGui.TextLabel.Text)+ a.Num.Value).Special.Value == false then 
							print("6a")
							print("FinishedTrue") --Print if it found
							if a.Owner.Value == nil	then --If it is the main
								a.Owner.Value = i --Set the main to the Line
								a:SetPrimaryPartCFrame(a.Owner.Value.PrimaryPart.CFrame) --Let it get the position
							else
								a.Owner.Value = game.Workspace.Map:FindFirstChild("" .. convert2(i.Letter.Value + a.Ltr.Value) .. "-" ..tonumber(i.NumberText.SurfaceGui.TextLabel.Text)+ a.Num.Value)
								a:SetPrimaryPartCFrame(a.Owner.Value.PrimaryPart.CFrame) --Let it get the position from the neighbor
							end
							a.Finished.Value = true --Make it Finished
							table.insert(RoomTable,a.Owner.Value.Name) --Add it
						end
					end
				end
			end	
			wait()
			if #RoomTable >= #Room:GetChildren() then --If it is finished
				for _,ac in pairs(Room:GetChildren()) do
					if ac.Finished.Value == true then
						Count += 1
						print(RoomTable[Count])
						if Count >= #RoomTable then
							print(#RoomTable)
							Finished = true 
							for _,acc in pairs(Room:GetChildren()) do
								acc.Owner.Value.Special.Value = true
							end
							break
							--Make the loop finished	
						end
					end
				end
			end
		end
	until Finished == true
end

the output is

  16:27:27.647  1a  -  Server - Loop:108
  16:27:27.648  Restart  -  Server - Loop:109
  16:27:27.683  2a  -  Server - Loop:112
  16:27:27.715  3a  -  Server - Loop:120
  16:27:27.715  4a  -  Server - Loop:122
  16:27:27.749  5a  -  Server - Loop:125
  16:27:27.782  6a  -  Server - Loop:127
  16:27:27.783  FinishedTrue  -  Server - Loop:128
  16:27:27.783  4a (x2)  -  Server - Loop:122
  16:27:27.932  1a  -  Server - Loop:108
  16:27:27.932  Restart  -  Server - Loop:109
  16:27:27.965  2a  -  Server - Loop:112
  16:27:27.998  3a  -  Server - Loop:120
  16:27:27.998  4a  -  Server - Loop:122
  16:27:28.031  5a  -  Server - Loop:125
  16:27:28.066  6a  -  Server - Loop:127
  16:27:28.066  FinishedTrue  -  Server - Loop:128
  16:27:28.098  4a (x2)  -  Server - Loop:122
  16:27:28.231  1a  -  Server - Loop:108
  16:27:28.231  Restart  -  Server - Loop:109
  16:27:28.264  2a  -  Server - Loop:112
  16:27:28.298  3a  -  Server - Loop:120
  16:27:28.298  4a  -  Server - Loop:122
  16:27:28.331  5a  -  Server - Loop:125
  16:27:28.364  6a  -  Server - Loop:127
  16:27:28.365  FinishedTrue  -  Server - Loop:128
  16:27:28.399  4a  -  Server - Loop:122
  16:27:28.432  5a  -  Server - Loop:125
  16:27:28.465  6a  -  Server - Loop:127
  16:27:28.465  FinishedTrue  -  Server - Loop:128
  16:27:28.499  4a  -  Server - Loop:122

It stops when it doesnt find the other source Ive tried everything, from searching all forums to rewriting the script more than I should.

2 Likes

Btw Ive tried while true do, setting wait everywhere settings break or recalling the function again nothing worked yet

did you fire the function “Gen”, im not the best at programming but it seems like from the code you need to fire it so it keeps repeating.

yes it got fired once I made a print so when its finished it prints (“Finished”) but it never printed so its stuck but it should restart


this is an image when it stopps working

–
grafik
this is how it should spawn in

hmmmm, try this:

local repeating = false

if repeating == false then
    repeating = true
    repeat wait()
       -- your function here
    until repeating == false
end

like i said, im not the best at programming, but i am trying to help.

that would make no sense cause then the loop would never stop and keep respawning even if its finished

hmmmmmm, well how many times would you want it to repeat before it stops?

till the generation is finished thats what my script should do but it doesnt re generate it just freezes

maybe this?

local repeating == false

while Finished == false do
   if repeating == false then
     repeat wait(5)
         -- function here
     until repeating == false and Finished == true 
 end

if that doesn’t work or make sense then I don’t think i can help on this one.

your script didnt work sadly :confused:

alright, well idk what to tell you. like i said before im not good at programming as well as other people are. I hope you get your script to work in the future!

Can you describe a little bit more about what this script should be doing? Based on the comments and script, is it suppose to spawn something in a specific order and place? I would like to help but the script looks a little too messy for me to understand.

1 Like

so the script should repeat until the 3 split up rooms find their position. it works all fine until it spawns next to wall, it breaks cause it cant go to A-0. the generation is like A-1/A-2/B-1/B-2 but once its at B-2 and a part right should spawn. instead of respawning the thing like normaly it freezes complete


what I was expecting it to do

It spawns in 1 room places it randomly checks if that position is taken by anything, if isnt then place, but if it is then replace the room. then take the other room. place it next to you. check if that position is taken. if it is then move the whole thing somewhere else. then do the same with right side. if 3=3 complete, then stop the generation and play the game.

Worked i just had to put else or not found and then break

for some reason it stopped working and now its glitching again…