Hello! So I’m making my first exp system and its going VERY well. I ran into a horrible problem where the gui either goes WAY over the limit or way under the limit that I need it to be. I’ve spent an hour on this and still don’t know what’s wrong.
local bar = script.Parent.EXPBar
local text = script.Parent.EXPLabel
local plr = game.Players.LocalPlayer
local level = plr.LevelInfo.Level
local exp = plr.LevelInfo.Experience
local expNeeded =math.floor(7 * 1.3 ^ level.Value)
text.Text = exp.Value.." / "..expNeeded
bar:TweenSize(UDim2.new(expNeeded/level.Value, 0, .19, 0), "In", "Linear", 0.3)
exp.Changed:Connect(function()
expNeeded = math.floor(7 * 1.3 ^ level.Value)
text.Text = exp.Value.." / "..expNeeded
bar:TweenSize(UDim2.new(expNeeded/level.Value, 0, .19, 0), "In", "Linear", 0.3)
end)
level.Changed:Connect(function()
expNeeded = math.floor(7 * 1.3 ^ level.Value)
text.Text = exp.Value.." / "..expNeeded
local change = exp.Value / expNeeded
bar:TweenSize(UDim2.new(change,0, .19, 0), "In", "Linear", 0.2)
end)
Here I see that you are putting your exp value as the X scale parameter within the UI: :TweenSize(UDim2.new(expNeeded/level.Value, 0, .19, 0), "In", "Linear", 0.3)
UDim2.new(X,_,_,_)
Instead you would need a ratio. First you would need to know how much exp you’ve earned then how much is the max then you would see how many times the “Earned XP” can go into the “Max XP” by divding. So your math should look like this: EarnedXP/MaxXP. Therefore when updating your UI use:
UDim2.new(EarnedXP/MaxXP, 0, .19, 0)
So a full bar would be UDim2.new(1,0,.19,0)' and a half bar would be UDim2.new(0.5,0,.18,0)
So we would want our XSize to be 1 or “bar full” when we have reached our max xp value. Or half “0.5” when we have reached the half way point towards max exp.
Ex: EarnedXP = 5, MaxXP = 10 '
EarnedXP/MaxXP = 0.5
I hope this makes sense. Let me know how it works out for you.
Thanks for the feedback. Now that I’m relooking at this I did take the long approach of explaining. In simple terms I could of just told you to that you should divide earnedXP/maxXP instead of expNeeded/level.Value. lol
Wouldn’t that be: local exp = plr.LevelInfo.Experience(The amount the player has earned.) local expNeeded = math.floor(7 * 1.3 ^ level.Value)(The amount to reach aka max)?
What I did for my system was determined the size of the bar, then did the bar size / the total XP to get a scale factor. Took that scale factor value and multiplied it by the XP that the player currently has and set that X value to the size.
Ok, I’ll bring in an example. Let’s say that the X size of the bar is 1, and the total XP for the level is 100, the scale factor is 1/100 (which is .01). Next, let’s say the player has an XP of 23, you take that current XP value of 23 and multiply the scale factor of 1/100 which will get you 23/100. Then, you’d set the X size of the bar to 23/100.