Alternative to spamming if's

So I am not sure if this even belongs here but here I go:

I am so sorry to those whose eyes are bleeding from this piece of code and I am with you too. I know that if statements aren’t necessarily “bad” but they aren’t very friendly to the human eyes. It hurts to look at this and I want to know if there is an efficient way of trying to clean this bloody mess?

(also what I am trying to do is that I check the level and rebirth and increase a stat cap)

ame.Players.PlayerAdded:Connect(function(player)
	player:WaitForChild("Data").Level.Changed:Connect(function()
		if player:WaitForChild("Data").Level.Value > 1000 and player:WaitForChild("Data2").Rebirth.Value == 0 then
			player:WaitForChild("Data").Level.Value = 1000
		elseif player:WaitForChild("Data").Level.Value > 2000 and player:WaitForChild("Data2").Rebirth.Value == 1 then
			player:WaitForChild("Data").Level.Value = 2000
		elseif player:WaitForChild("Data").Level.Value > 3000 and player:WaitForChild("Data2").Rebirth.Value == 2 then
			player:WaitForChild("Data").Level.Value = 3000
		elseif player:WaitForChild("Data").Level.Value > 4000 and player:WaitForChild("Data2").Rebirth.Value == 3 then
			player:WaitForChild("Data").Level.Value = 4000
		elseif player:WaitForChild("Data").Level.Value > 5000 and player:WaitForChild("Data2").Rebirth.Value == 4 then
			player:WaitForChild("Data").Level.Value = 5000
		elseif player:WaitForChild("Data").Level.Value > 6000 and player:WaitForChild("Data2").Rebirth.Value == 5 then
			player:WaitForChild("Data").Level.Value = 6000
		elseif player:WaitForChild("Data").Level.Value > 7000 and player:WaitForChild("Data2").Rebirth.Value == 6 then
			player:WaitForChild("Data").Level.Value = 7000
		elseif player:WaitForChild("Data").Level.Value > 8000 and player:WaitForChild("Data2").Rebirth.Value == 7 then
			player:WaitForChild("Data").Level.Value = 8000
		elseif player:WaitForChild("Data").Level.Value > 9000 and player:WaitForChild("Data2").Rebirth.Value == 8 then
			player:WaitForChild("Data").Level.Value = 9000
		elseif player:WaitForChild("Data").Level.Value > 10000 and player:WaitForChild("Data2").Rebirth.Value == 9 then
			player:WaitForChild("Data").Level.Value = 10000			
		elseif player:WaitForChild("Data").Level.Value > 11000 and player:WaitForChild("Data2").Rebirth.Value == 10 then
			player:WaitForChild("Data").Level.Value = 11000
		end
	end)
	player:WaitForChild("Data2").Resilience.Changed:Connect(function()
		if player:WaitForChild("Data2").Resilience.Value > 40 and player.Data2.Rebirth.Value == 0 then
			player:WaitForChild("Data2").Resilience.Value = 40
		elseif player:WaitForChild("Data2").Resilience.Value > 50 and player.Data2.Rebirth.Value == 1 then
			player:WaitForChild("Data2").Resilience.Value = 50
		elseif player:WaitForChild("Data2").Resilience.Value > 60 and player.Data2.Rebirth.Value == 2 then
			player:WaitForChild("Data2").Resilience.Value = 60
			
		elseif player:WaitForChild("Data2").Resilience.Value > 70 and player.Data2.Rebirth.Value == 3 then
			player:WaitForChild("Data2").Resilience.Value = 70
			
		elseif player:WaitForChild("Data2").Resilience.Value > 80 and player.Data2.Rebirth.Value == 4 then
			player:WaitForChild("Data2").Resilience.Value = 80
			
		elseif player:WaitForChild("Data2").Resilience.Value > 90 and player.Data2.Rebirth.Value == 5 then
			player:WaitForChild("Data2").Resilience.Value = 90
			
		elseif player:WaitForChild("Data2").Resilience.Value > 100 and player.Data2.Rebirth.Value == 6 then
			player:WaitForChild("Data2").Resilience.Value = 100
			
		elseif player:WaitForChild("Data2").Resilience.Value > 110 and player.Data2.Rebirth.Value == 7 then
			player:WaitForChild("Data2").Resilience.Value = 110
				
		elseif player:WaitForChild("Data2").Resilience.Value > 120 and player.Data2.Rebirth.Value == 8 then
			player:WaitForChild("Data2").Resilience.Value = 120
			
		elseif player:WaitForChild("Data2").Resilience.Value > 130 and player.Data2.Rebirth.Value == 9 then
			player:WaitForChild("Data2").Resilience.Value = 130
				
		elseif player:WaitForChild("Data2").Resilience.Value > 140 and player.Data2.Rebirth.Value == 10 then
			player:WaitForChild("Data2").Resilience.Value = 140
			
		end
	end)
end)

Just put in some math functions. It seems that the base level cap is 1k, +1k per rebirth. So just set the level cap to (rebirths + 1) * 1k. Then work something similar out for your other stuff.

“Well, you can never have too many if statements.” - YandereDev

16 Likes

What is said in the first reply should work, but I also want to say that if you have a lot of duplicates of one thing, just make a variable, it will be way less work.

Ok, so.

First of all, I believe that there is an error in your script. Your script will first detect if the player is above level 40. The problem is that, if the player is above level 40 (let’s say 98), the script would put the level as 40 and not 90, as it knows that 90 is above 40, and not their actual level. So you would have to make “everything upside down”.

Deleted the code because I realized that I was overcomplicated myself. :flushed:

Using too many if statements can slow down workflow and production. It’d be better to have a simpler / more efficient solution than using loads of if statements.

2 Likes

In addition to @lyookachu noting that it’s upside down, in the future, you might not want to be inflexible to
to formula changes (your large if-elseif block has this advantage by allowing simple edits for value changes.)
You could loop over a table such as:

local t = {  { level = 11000, rebirth = 10, newlevel = 11000 },
             { level = 10000, rebirth =  9, newlevel = 10000 },
--[[ etc. ]] }

1 Like

I feel like you guys are over-complicating this.

This looks solvable with an equation ((rebirths + 1) * 1000), but for me, if you ever have more than about 2 elseif statements, look into dictionaries or switch case statements (oh wait lua doesn’t have these)

1 Like

I mean, it kinda sorta does (not exactly, but meh). There’s also some metatable stuff that I can’t be bothered to look into.

Wait, I think a for loop would work.

I like the equation idea some ways up and your code fits well enough for that to happen. You also use too much WaitForChild which is expensive and not necessary. Highly recommend you change to use an equation and use the minimum value.

For rebirths, ((rebirth + 1) * 1000 seems to be your level cap.

local Level = Data.Level
local Rebirths = Data2.Rebirth

Level.Changed:Connect(function ()
    -- Select the lowest value of all arguments passed
    Level.Value = math.min(Level.Value, (Rebirths.Value + 1) * 1000)
end)

The same logic should be applicable for resilience: if you can derive a formula, use it.

3 Likes