How to avoid problem with script that's messier every line of code?

Script gets messier every single time i’m trying to add new mechanic and I don’t know how to make it simpler
I can’t orientate in my script beacuse function is so big and messy

function GenerateStep (cells)
	local NewMap = {}
	for x = 1,Xs do
		for z = 1,Zs do
			local myName = x.."."..z
			local myCell = cells:FindFirstChild(myName)
			if myCell then
				local myColor = myCell.BrickColor
				local myArmy = myCell.Army
				local myDisplay = myCell.ArmyDisplayed
				local myMaterial = myCell.Material
				local moved = myCell.HasMoved
				if moved.Value == false then
					moved.Value = true
					local moves = 0
					local freeCells = {}
					for xM = -maping,maping do
						for zM = -maping,maping do
							local name = (x+xM).."."..(z+zM)
							local cell = cells:FindFirstChild(name)
							if cell and myCell ~= cell  then
								local color = cell.BrickColor
								if color ~= myColor and myColor ~= BrickColor.White() then
									moves += 1
									freeCells[moves] = name
								end
							end
						end
					end
					if freeCells[1] ~= nil then
						local cell = freeCells[math.random(1,#freeCells)]
						if NewMap[cell] == nil then
							local enemyarmy = cells:FindFirstChild(cell):FindFirstChild("Army")
							local capital = cells:FindFirstChild(cell):FindFirstChild("Castle_Model")
							local material = cells:FindFirstChild(cell).Material
							if myArmy.Value > 1 and enemyarmy then
								if material ~= water then
									local add = 0
									local minusesForCapital = 1
									local isCapital = false
									local mountainMinuses = 1
									if myCell.Position.Y < enemyarmy.Parent.Position.Y then
										mountainMinuses = (enemyarmy.Parent.Position.Y-myCell.Position.Y) * 10
									end
									if capital then
										minusesForCapital = math.random(5,10)
										isCapital = true
									end
									if enemyarmy.Value <= 1 then
										if isCapital == true then
											annex(capital.Parent.BrickColor,myColor)
											capital:Destroy()
										end
										add = myArmy.Value
										myArmy.Value = 1
										enemyarmy.Value = add
										NewMap[cell] = myColor
										enemyarmy.Parent.BrickColor = myColor
										moved.Value = true
									else
										local s1 = myArmy.Value
										local s2 = enemyarmy.Value
										myArmy.Value -= (math.random(s2)*minusesForCapital)*mountainMinuses
										enemyarmy.Value -= (math.random(s1)/minusesForCapital)/mountainMinuses
										add = myArmy.Value
										if enemyarmy.Value <= 0 and myArmy.Value > 1 then
											local moved = enemyarmy.Parent.HasMoved
											if isCapital == true then
												annex(capital.Parent.BrickColor,myColor)
												capital:Destroy()
											end
											local chance = math.random(1,5)
											myArmy.Value = myArmy.Value / chance
											enemyarmy.Value = add - 1
											enemyarmy.Parent.BrickColor = myColor
											NewMap[cell] = myColor
											moved.Value = true
										end
									end
								else
									if enemyarmy then
										local s1 = myArmy.Value
										local s2 = enemyarmy.Value
										myArmy.Value -= (math.random(s2))
										enemyarmy.Value -= (math.random(s1))
										local add = myArmy.Value
										if enemyarmy.Value <= 0 and myArmy.Value > 1 then
											local moved = enemyarmy.Parent.HasMoved
											local chance = math.random(1,5)
											myArmy.Value = 0 
											enemyarmy.Value = add - 1
											enemyarmy.Parent.BrickColor = myColor
											NewMap[cell] = myColor
											moved.Value = true
										end
									else
										myArmy:Clone().Parent = cells:FindFirstChild(cell)
										myDisplay:Clone().Parent = cells:FindFirstChild(cell)
										moved:Clone().Parent = cells:FindFirstChild(cell)
										if myMaterial == water then
											myArmy:Destroy()
											myDisplay:Destroy()
											moved:Destroy()
										else
											myArmy.Value = 1
										end
									end
									local bill = enemyarmy.Parent.ArmyDisplayed
								end
							elseif enemyarmy == nil and myArmy.Value > 1 then
								local material = cells:FindFirstChild(cell).Material
								local Cell = cells:FindFirstChild(cell)
								if myCell.Material == water then
									local boat = myCell:FindFirstChild("BoatModel")
									if boat then
										boat:Destroy()
									end
								end
								if material == water then
									Cell.BrickColor = myColor
									myArmy.Parent = Cell
									myDisplay.Parent = Cell
									NewMap[cell] = myColor
								end
							end
						end
					else
						local freeCells = {}
						local moves = 0
						local freeCells = {}
						for xM = -maping,maping do
							for zM = -maping,maping do
								local name = (x+xM).."."..(z+zM)
								local cell = cells:FindFirstChild(name)
								if cell and myCell ~= cell  then
									local color = cell.BrickColor
									if color == myColor or myColor == BrickColor.Blue() then
										moves += 1
										freeCells[moves] = name
									end
								end
							end
						end
						if freeCells[1] ~= nil then
							local cell = freeCells[math.random(#freeCells)]
							if cell then
								local val1 = cells:FindFirstChild(cell).Army
								if val1 then
									val1.Value += myArmy.Value
									myArmy.Value = 1
									local disp = cell.ArmyDisplayedTextLabel
									update(disp,val1.Value)
									update(myDisplay,myArmy.Value)
								end
							end
						end
					end
				end
			end
		end
	end

are you asking people to help you make your code cleaner if it is i think it should be in #help-and-feedback:code-review

Hello what I often do is create more functions and just call it inside the main function :wink:.

Sure i’ll try to make smaller local functions :slight_smile:

1 Like