Help with a math formula for EXP Bar GUI 0%-100% X Scaling

So, I have this Experience Bar GUI that tweens the Filler inside of it when a Player awarded a certain Exp value or getting leveled up. The values are working. But now, I didn’t how to do the formula to formulate the current MaxValue to represent as the 1 or 100% of the Filler’s X scale because of the MaxValue changes when the Player has leveled up.

PS: I don't know the term for the title so I didn't know what to search to get the idea before making this topic (How do I use a blurring method instead of this red highlight?)

Can we see your current script please. Also can’t you just reference the MaxValue inside of the function?

The only script that I have for now is for LevelingUp and EXP only.

Small Script:

-- PlayerAdded
local Level = Player.leaderstats.Level
local Exp = Player.HiddenStats.Exp

Exp:GetPropertyChangedSignal("Value"):Connect(function()
    local maxExp = 100 * (Level.Value + 1) -- Simple formula
    if Exp.Value >= maxExp then
        Level.Value += 1
        Exp.Value = 0
    end
end)

You must first determine a rough relation between the exp and level. For example, for our case, we will say that 250 000 exp is equal to 100 levels. Now we can calculate an equation based on this. The best way would be to use the radical or the square root equation because you want the player to level up slower as they progress:

Radical Function:
image

Finding the value of a:
image
image

If you sub in your exp amount:
image

Now in your code, all you have to do is: determine the amount of exp the player has and calculate their level using the function. Say if they have 5000 exp. You would get something like f(5000) ≈ 14.14, then using the math.modf method, you can get the scale of the UI. So in the script:

local function calculate(exp)
    return math.modf((1/5) * math.sqrt(exp))
end

local Level, UIScale = calculate(5000)
print(Level, UIScale)

--Output: 14 0.14213562373095
1 Like

@GrayzcaIe’s post seems interesting but I think you’re just asking how to map one range of numbers to another range of numbers?

local function MapRange (x, in_min, in_max, out_min, out_max)
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
end

-- map 500 on the scale [250, 750] to the scale [0, 100]
print(MapRange(500, 250, 750, 0, 100)) --> 50 (%)

Taken from: Javascript Map range of number to another range · GitHub

Also

Click the gear icon and choose “Blur Spoiler”.

[spoiler]Or do this[/spoiler] so it shows up like this

3 Likes

Sorry for this but can you explain to me what are the parameters are for? Can you simplify the parameter’s name it for me like ExpValue. (Not an advance scripter/math wizard)

For @GrayzcaIe reply, I think that’s not what I mean. My only goal is to set the current max value for Exp as the 1.0 of the X.Scale of the Filler.

Thanks for the tip for this blur method

So you wanted to scale the bar based on total exp? So if you had 50% of the max exp, would the exp bar be 0.5?

Yes, that’s what I mean! But I don’t understand the proper logic for it.

In that case the MapRange function mentioned by @nicemike40 will work nicely. Let me re-write the function to help you better understand it:

local function MapRange (CurrentEXP, MinEXP, MaxEXP, MinUIScale, MaxUIScale)
  return (CurrentEXP - MinEXP) * (MaxUIScale - MinUIScale) / (MaxEXP - MinEXP) + MinUIScale;
end

MinEXP and MinUIScale would be zero always since they start at 0. MaxUIScale would always be 1. All you have to do is pass in the MaxEXP and the CurrentEXP.

4 Likes