You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
Im Making An Algorythm That Generates mazes
What is the issue?
It Works But Just Randomly Stops
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
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.