Why does Code Just Stop Working

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

  1. What do you want to achieve?
    Im Making An Algorythm That Generates mazes

  2. What is the issue?
    It Works But Just Randomly Stops

  3. What solutions have you tried so far?
    I Tried Simplifying The Code And Even Rewritting some Bits

this is the code

while wait() do
--for t = 1,1000 do
	for i,v in pairs(game.Workspace.Maze:GetChildren()) do
		local attachment2 = v:FindFirstChild("Attachment")
		local parts = game.ReplicatedStorage.MazeParts:GetChildren()
		local order = math.random(1,3)
		--for i,v in pairs(v:GetChildren()) do
			--if v.Name == "Attachment" then
				--print("Attachment")
				
			--end
		--end

		if attachment2 ~= nil then
			newpart = parts[order]:Clone()
			v.Attachment.Orientation += Vector3.new(0,180,0)
			local attachment2 = v.Attachment 
			local attachment1 = newpart.ConnectionAttachment
			--Result1
			local RayOrigin1 = attachment2.WorldPosition
			local RayDirection1 = attachment1.WorldCFrame.LookVector
			local Length = newpart.Size.Z
			local resultZ = workspace:Raycast(RayOrigin1, RayDirection1 * Length)
			local RayOrigin2 = attachment2.WorldPosition
			local RayDirection2 = attachment1.WorldCFrame.LookVector
			local Width = newpart.Size.X
			local resultX = workspace:Raycast(RayOrigin1, RayDirection2 * Width)
			if resultZ then
				newpart:Destroy()
			elseif resultX then
				
				newpart:Destroy()
			else
				--Result2
			
			
				newpart.Parent = game.Workspace.Maze
				newpart.CFrame = attachment2.WorldCFrame * attachment1.CFrame:Inverse()
				attachment2:Destroy()
						--ammount = 0 
						wait(0.25)				
				end
			end
		end	
			
		end

	

for i,v in pairs Is not an infinite loop. Basically you run out of Maze children, so the code has nothing to work with anymore

but than it gets repeated by while wait() do due to if it didn’t it would most likely only do it once

Ah yes, You’re right. Put a Print statement in your loop to see if it stops printing when it stops working, then we can narrow the issue down. Also I assume there are no output errors?

there used to be but me simplifying the code fixed it bc basicly the code did to much stuff making it not able to catch up

image
after it decided to stop it just sent print alot of times without it rlly doing anything
this is where print’s are
image

You have

if attachment2 ~= nil then

But then, you are doing

attachment2:Destroy()

So eventually, attachments of all of your parts are getting destroyed, and if statement starts returning false, thats why script stops doing anything.
To confirm this you can put a print statement directly under if attachment2 ~= nil, and see.

The issue is, you are destroying the attachment of original parts. Instead of it you need to destroy attachments of clones.

the issue is i can still see attachments in most of the parts
let me show by making teh attachment visible

Just put a print statement where I said

You can see the attachments of cloned parts, but the originals dont have attachments anymore

image

After the script stops working, check the attachments of the original parts in the workspace. Under Maze children

it doesn’t rlly mind if the attachment on original part is gone due to the attachment2 variable changing each time due to its ussing attachment2 to attach into attachment1

change this to

local attachment2 = newpart.Attachment

but than won’t the connectionattachment get connected to the attachment itself due to it would be same attachment

before this line put

print(attachment2)

and after this line put

print(resultZ, resultX)

does it keep printing after maze stops building

it does
image

could you show what is inside these parts

image