How to make a number loop each strength you earn

  1. What do you want to achieve?
    I need a help to make converting the Strength to 1, 2, 3, 4, Etc. each stage per strength.

  2. What is the issue?
    My brain won’t work on how to make a infinite loop to increase the number value each time you reach the final strength.

  3. What solutions have you tried so far?
    I looked everywhere in every devforum and i couldn’t find any solution about increasing number.

Let’s say the Strength is 75, 150, 300, 600, and so on. This keeps going.
And then the number also keeps converting and adding itself: 1, 2, 3, 4, 5, 6…
But when the strength is reset to 0 or below 75, the CurrentSword is also set back to 1

You can change unnecessary functions in the script, because I was just trying to mess around with it.

Here’s the script that i made:

local function Divison(Strength : number, CurrentSword : number)
	local Result
	
	local Symbolize = math.floor(Value / 75)
	local PartOfLife = 1 -- This is the CurrentSword that i want to return
	

	if Value >= 300 then
		PartOfLife = math.floor(Value / 75) + 1 -- 4
	elseif Value >= 150 then
		PartOfLife = math.floor(Value / 75) + 1 -- 3
	elseif Value >= 75 then
		PartOfLife = math.floor(Value / 75) + 1 -- 2
	elseif Value < 75 then
		PartOfLife = 1 -- 1
	end
	
	Result = PartOfLife

	return Result
end

So uh how i want it to work is let’s say
when the player reached 75 Strength, the Strength converts to 1,
and then once the Strength reached to 150, it converts to 2,
and then once the strength reached 300, it converts to 3,
and then once the Strength reached 600, it converts to 4.

Why do i want to do this? Less coding for more improvements. Since i’ve made the hardest loop earlier, now i can’t cuz i used all in my brain and now it’s not working XD.

1 Like

Instead of making use of a loop, you could instead make use of math.log, with a base of 2:

PartOfLife = math.floor(math.log(Value / 75, 2) + 1)

Note that this breaks when Value is less than 75, so just force it to be at the lowest 1:

PartOfLife = math.floor(math.log(math.max(1, Value / 75), 2) + 1)

It has to clamp the value to be above 1 inside the log function, otherwise it will break mathematically.

1 Like