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

		if attachment2 ~= nil then
			newpart = parts[order]:Clone()
			v.Attachment.Orientation += Vector3.new(0,180,0)
			local attachment2 = v.Attachment 
			local attachment1 = newpart.ConnectionAttachment
			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
			elseif resultX then
				newpart.Parent = game.Workspace.Maze
				newpart.CFrame = attachment2.WorldCFrame * attachment1.CFrame:Inverse()
						--ammount = 0 


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

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

You have

if attachment2 ~= nil then

But then, you are doing


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


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


and after this line put

print(resultZ, resultX)

does it keep printing after maze stops building

it does

could you show what is inside these parts