Random maze generator isnt backing up when there is no more parts to go to

so i am trying to make a random maze gen but my backing up script part isnt working
here is the backing up script part

				if StartTick then
					if math.floor(tick()-StartTick) > 1 then
						print("E",CurrentParts)
						script.Parent.Position = OldPositions[#OldPositions]
						table.remove(OldPositions,#OldPositions)
						StartTick = math.round(tick())
						print("No Part Found..Backing Up")
					elseif #OldPositions == 0  then
						Finished = true
						for i,v in pairs(parttoputback) do
							v.Parent = game.Workspace.Walls
						end
						for i,v in pairs(game.Workspace.Walls:GetChildren()) do
							v.Size = v.Size +Vector3.new(1,0,0)
						end
						print("Maze Was Finished In "..math.round(tick() - Elapsed).." Seconds")
	
					end
			end
Full script
-- Ray
local ignoreinstance = {script.Parent}
local TS = game:GetService("TweenService")
local Info = TweenInfo.new(0.01,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
local Part = script.Parent
local TotalParts = #game.Workspace.Parts:GetChildren()
local CurrentParts = 0
function IsEven(num)
	return num % 2 == 0
end
for i=2,1,-1 do
	print("Starting In "..i)
	wait(1)
end
print("Maze Generation Started")

local Finished = false
repeat
	Elapsed = tick()
	wait()
until Elapsed ~= nil

local Pos = {
	Vector3.new(0,0,-10),
	Vector3.new(-10,0,0),
	Vector3.new(0,0,10),
	Vector3.new(10,0,0)
}

local parttoputback = {}

local OldPositions = {}


while not Finished do
	for i,v in pairs(Pos) do
		script.Parent.Orientation = Vector3.new(0,i*90,0)
		local ray = Ray.new(script.Parent.Position,Pos[i])
		local part, position = workspace:FindPartOnRayWithIgnoreList(ray,ignoreinstance,false,true)
		if part then
				local selected = math.random(1,2)
				if part.Name == "Wall" and selected == 1 then
					part.Parent = game.ReplicatedStorage
					wait(0.1)
					local ray2 = Ray.new(script.Parent.Position,Pos[i])
					local part2, position2 = workspace:FindPartOnRayWithIgnoreList(ray2,ignoreinstance,false,true)
					part.Parent = game.ReplicatedStorage
					if part2 ~= nil and part2.Name == "MazePart" then
						repeat
							StartTick = math.round(tick())
							wait()	
						until StartTick ~= nil
						print("A Maze Part Was Found!")
						part:Destroy()
						PrevPartPosition = script.Parent.Position
						local tween = TS:Create(script.Parent,Info,{Position = part2.Position})
						tween:Play()
						local Distance = math.round((part2.Position - PrevPartPosition).Magnitude)
						local DistancePart = Instance.new("Part",game.Workspace.Path)
						DistancePart.Material = Enum.Material.SmoothPlastic
						DistancePart.CanTouch = false
						DistancePart.CanCollide = false
						DistancePart.Anchored = true
						local Orientation = Part.Orientation.Y
						DistancePart.Orientation = Vector3.new(0,Orientation,0)
						DistancePart.Size = Vector3.new(16.001,1,6)
						if Orientation == -90 then
							DistancePart.Position = PrevPartPosition+Vector3.new(0,0,(Distance/2))
						elseif Orientation == 90 then
							DistancePart.Position = PrevPartPosition-Vector3.new(0,0,(Distance/2))	
						elseif Orientation == -180 or Orientation == 180 then
							DistancePart.Position = PrevPartPosition-Vector3.new((Distance/2),0,0)		
						elseif Orientation == 0 then
							DistancePart.Position = PrevPartPosition+Vector3.new((Distance/2),0,0)
						end
						DistancePart.Position = Vector3.new(math.round(DistancePart.Position.X),DistancePart.Position,math.round(DistancePart.Position.Z))
						CurrentParts = CurrentParts + 1
						PrevPartPosition = part.Position
						table.insert(OldPositions,#OldPositions+1,part2.Position)
						part2:Destroy()
					else
						print("No Maze Part There!")
						table.insert(parttoputback,#parttoputback+1,part)
					end
				end
				if StartTick then
					if math.floor(tick()-StartTick) > 1 then
						print("E",CurrentParts)
						script.Parent.Position = OldPositions[#OldPositions]
						table.remove(OldPositions,#OldPositions)
						StartTick = math.round(tick())
						print("No Part Found..Backing Up")
					elseif #OldPositions == 0  then
						Finished = true
						for i,v in pairs(parttoputback) do
							v.Parent = game.Workspace.Walls
						end
						for i,v in pairs(game.Workspace.Walls:GetChildren()) do
							v.Size = v.Size +Vector3.new(1,0,0)
						end
						print("Maze Was Finished In "..math.round(tick() - Elapsed).." Seconds")
	
					end
			end
		end
	end
	print(Finished)
		wait(0.2)
end
-- Ray

There are no errors just when the time for backing up has come the script stops for some reason and dosent backs up yesterday it was working but today its not why?

1 Like