Experience Bar Level Up Problems

So I just recently made an XP Bar and it’s great and all but when it comes to leveling up it gets kinda weird, i’m using a click detector right now to just test and I try to add 1k xp and it does this to it


Here is the code

while _G.PlayerData[player.UserId]["EXP"] >= _G.PlayerData[player.UserId]["Max"] do
	game:GetService("RunService").Stepped:Wait()
	_G.PlayerData[player.UserId]["Level"] += 1
	_G.PlayerData[player.UserId]["EXP"] = _G.PlayerData[player.UserId]["EXP"] - _G.PlayerData[player.UserId]["Max"]
	_G.PlayerData[player.UserId]["Max"] = _G.PlayerData[player.UserId]["Level"] * 150
	Level.Text = "Level : ".._G.PlayerData[player.UserId]["Level"]
	ExperienceLabel.Text = _G.PlayerData[player.UserId]["EXP"].."/".._G.PlayerData[player.UserId]["Max"]
	TMod:DoTween():Play(ExperienceBar,.25, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut, 0, false, 0, {Size = UDim2.new(math.clamp(_G.PlayerData[player.UserId]["EXP"],0,1),0,1,0)})
end

Ik this is off topic but why do you have _G what is it used for?

So basically, I want to access my playerdata throughout every single server script so I pair it with _G so I can retrieve, modify the data anywhere I want too! Plus it makes it so much easier to save data when its all organized like so

If I can remember, _G means Global Variable. but dont quote me on it.

2 Likes

Yes
⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀

I think the problem is: your using a While Loop, so your Continuesly adding XP.
Maybe change it to an “If Statement”.

Or add a return to the end of the while loop.

Thanks (30 chaaaaaaaaaaaaaaaaaar)

1 Like

No problem ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀ ⠀

I tried that and it still didnt seem to work and sometimes it bugs and the UI doesnt change for the next level

1 Like

Except the while loop has a conditional statement that must be meet for it to run. So it’s not continuously adding experience. I think it would be obvious to spot if that was the issue.

I’ve never used _G before since I’ve heard it has negatives to it that in many cases out weigh the positives so I was just curious why you (@loveicicIe ) are using _G over storing player’s data in ModuleScripts. Since I thought a big positive of using ModuleScripts is you could create functions to award player experience instead of using a constant loop.

2 Likes

Basically Im not a big fan of storing data in actual objects in explorer so, I tied _G to a table called PlayerData and I can use it across all server scripts where I need to use them, this makes datastores much easier for this kind of structure

Oh I just fixed the problem thanks for helping!

1 Like