Equipment Upgrade

I am working on an upgrade system and created this setup with a bunch of if statements and else variables and was thinking there must be a better way to do this. If anyone has any ideas how I could improve upon this code it would be very helpful.

if selected.Name == "ProtonPack" then
				local protonPackUI = script.Parent.Parent.Parent.ProtonPack
				if upgradeTable.ProtonPack.Hydrogen["Lvl 1"].Purchased.Value == true and plrValues.Stats.Level.Value >= upgradeTable.ProtonPack.Hydrogen["Lvl 2"].lvlRequirement.Value and upgradeTable.ProtonPack.Hydrogen["Lvl 2"].Purchased.Value == false then
					protonPackUI.ScrollingFrame.hydrogenCapacity.TextLabel.Text = "Hydrogen Capacity Lvl 2"
				elseif upgradeTable.ProtonPack.Hydrogen["Lvl 2"].Purchased.Value == true and plrValues.Stats.Level.Value >= upgradeTable.ProtonPack.Hydrogen["Lvl 3"].lvlRequirement.Value and upgradeTable.ProtonPack.Hydrogen["Lvl 3"].Purchased.Value == false then
					protonPackUI.ScrollingFrame.hydrogenCapacity.TextLabel.Text = "Hydrogen Capacity Lvl 3"
				elseif upgradeTable.ProtonPack.Hydrogen["Lvl 3"].Purchased.Value == true and plrValues.Stats.Level.Value >= upgradeTable.ProtonPack.Hydrogen["Lvl 4"].lvlRequirement.Value and upgradeTable.ProtonPack.Hydrogen["Lvl 4"].Purchased.Value == false then
					protonPackUI.ScrollingFrame.hydrogenCapacity.TextLabel.Text = "Hydrogen Capacity Lvl 4"
				elseif upgradeTable.ProtonPack.Hydrogen["Lvl 4"].Purchased.Value == true and plrValues.Stats.Level.Value >= upgradeTable.ProtonPack.Hydrogen["Lvl 5"].lvlRequirement.Value and upgradeTable.ProtonPack.Hydrogen["Lvl 5"].Purchased.Value == false then
					protonPackUI.ScrollingFrame.hydrogenCapacity.TextLabel.Text = "Hydrogen Capacity Lvl 5"
				end
			end

So from what I see, if you purchase some Lvl 1, your Level is high enough to buy Lvl 2 and it hasn’t been purchased yet then change some text.

You could try this:

if selected.Name == "ProtonPack" then
	local protonPackUI = script.Parent.Parent.Parent.ProtonPack
	local hydrogenUpgrades = upgradeTable.ProtonPack.Hydrogen
	local playerLevel = plrValues.Stats.Level.Value

	for i = 1, 5 do
		local current = hydrogenUpgrades["Lvl " .. i]
		local nextUpgrade = hydrogenUpgrades["Lvl " .. (i + 1)]

		if current and nextUpgrade then
			if current.Purchased.Value == true and playerLevel >= nextUpgrade.lvlRequirement.Value and nextUpgrade.Purchased.Value == false then
				protonPackUI.ScrollingFrame.hydrogenCapacity.TextLabel.Text = "Hydrogen Capacity Lvl " .. (i + 1)
				break
			end
		end
	end
end

Just update the number in i when you add more levels. Hope this works!

(p.s, sounds like a cool game! what’s it about?

2 Likes