How can i clean my code?

Hello!

I’m working on an incremental game inspired by Grass Cutting Incremental. This code is the upgrade shop where you can buy upgrades and currently this is a mess and i want to make it cleaner.

I have tried so far functions and parameters.

Anyways, how can i make it cleaner? Also this is my first post! (Sorry for bad English)

local Players = game:GetService("Players")
local Short = require(game.ReplicatedStorage.Assets.Modules.Short)

local module = {}

function module.Shop()
	local player = Players.LocalPlayer
	
	local pickUpg = require(script.PickaxeUpg)
	
	local cash = player.leaderstats.Currency.Cash

	for i, v in pairs(script.Parent.Parent.UpgradeShop:GetChildren()) do
		if v:IsA("SurfaceGui") then
			for i, v in pairs(v.MainFrame.ScrollingFrame:GetChildren()) do
				if v:IsA("TextButton") then
					local upgrade = pickUpg[v.Name]
					
					local function amount(boostAmount, boostType)
						if v.Image.BuyAmount.Text == "x1" then
							boostType.Value += boostAmount	
							upgrade.Boost += boostAmount
							upgrade.Current += 1
						elseif v.Image.BuyAmount.Text == "x10" then
							boostType.Value += boostAmount * 10
							upgrade.Boost += boostAmount * 10
							upgrade.Current += 10
						elseif v.Image.BuyAmount.Text == "x100" then
							boostType.Value += boostAmount * 100
							upgrade.Boost += boostAmount * 100
							upgrade.Current += 100
						end
					end
					
					local function updateText()
						v.UpgAmount.Text = upgrade.Current .. " / " .. upgrade.Max
						
						if upgrade.Multi == true then
							v.InfoFrame.UpgPrice.Text = Short.en(math.round(upgrade.Price)) .. "$" .. " | " .. upgrade.Boost .. "x"
						else
							v.InfoFrame.UpgPrice.Text = Short.en(math.round(upgrade.Price)) .. "$" .. " | " .. upgrade.Boost .. "+"
						end				

						v.InfoFrame.UpgDescFrame.UpgDesc.Text = upgrade.Description
						v.InfoFrame.UpgName.Text = upgrade.Name
					end

					v.MouseButton1Click:Connect(function()	
						if upgrade.Current < upgrade.Max then
							if cash.Value >= upgrade.Price then
								cash.Value -= upgrade.Price
								updateText()
								if upgrade.Name == "Ore Value" then
									amount(1, player.leaderstats.Boosts.CashBoost)
									upgrade.Price = 10 * (1 + upgrade.Current) * (1.12 ^ upgrade.Current)
								elseif upgrade.Name == "EXP Value" then
									amount(1, player.leaderstats.Boosts.EXPBoost)
									upgrade.Price = 15 * (1 + upgrade.Current) * (1.2 ^ upgrade.Current)
								elseif upgrade.Name == "Dmg Value" then
									amount(1, player.leaderstats.Boosts.DmgBoost)
									upgrade.Price = 30 * (1 + upgrade.Current) * (1.3 ^ upgrade.Current)
								elseif upgrade.Name == "Pickaxe Value" then
									amount(10, player.leaderstats.PickStats.CashValue)
									upgrade.Price = 52 * (1 + upgrade.Current) * (1.12 ^ upgrade.Current)
								elseif upgrade.Name == "Pickaxe EXP" then
									amount(10, player.leaderstats.PickStats.EXPValue)
									upgrade.Price = 64 * (1 + upgrade.Current) * (1.15 ^ upgrade.Current)
								elseif upgrade.Name == "Pickaxe Dmg" then
									amount(10, player.leaderstats.PickStats.DmgValue)
									upgrade.Price = 70 * (1 + upgrade.Current) * (1.36 ^ upgrade.Current)
								end
								updateText()
								if upgrade.Current == upgrade.Max then
									if upgrade.Multi == true then
										v.InfoFrame.UpgPrice.Text = '<font color=\"rgb(170, 0, 0)\">' .. "Maxed" ..  '</font>' .. " | " .. upgrade.Boost .. "x"
									else
										v.InfoFrame.UpgPrice.Text = '<font color=\"rgb(170, 0, 0)\">' .. "Maxed" ..  '</font>' .. " | " .. upgrade.Boost .. "+"
									end
								end
							end
						end
					end)
					updateText()
				end
			end
		end
	end
end

return module

Any help is appreciated.

Thanks.

Hi,

The biggest thing within your code I would change is the usage of elseif statements, I would put it into a table instead to save lines and make it easier to read. Also, formatting your code is quite important readability-wise for the future when you want to change something.

Instead of doing the amount function, just do:

boostType.Value += boostAmount * tonumber(string.gsub(v.Image.BuyAmount.Text, "x", ""))
upgrade.Boost += boostAmount * tonumber(string.gsub(v.Image.BuyAmount.Text, "x", ""))
upgrade.Current += boostAmount * tonumber(string.gsub(v.Image.BuyAmount.Text, "x", ""))

Edit (literally 4 months later): This takes in more horizontal space and it actually may be more slower, but it takes up less lines now without ifs and elseifs.

Thank you so much, my code is much cleaner than before!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.