So I have a script that’s suppose to update the player’s level on the GUI above their head, but for some reason it won’t change above their head until their character is reset or reloaded (it changes on the leaderboard instantly).
Code:
local function calculateXpRequired(level)
return math.floor(500 * (1.25^(level - 1)))
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
level.Value = 1
local Xp = Instance.new("IntValue")
Xp.Name = "Xp"
Xp.Parent = leaderstats
Xp.Value = 0
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = 0
local characterBar = game.ServerStorage.CharacterBar
local levelText = characterBar.levelText
local titleText = characterBar.titleText
levelText.Text = "Lvl " .. tostring(level.Value)
if level.Value >= 1 and level.Value <= 9 then
titleText.Text = "Light Flyweight"
titleText.TextColor3 = Color3.fromRGB(173, 216, 230)
elseif level.Value >= 10 and level.Value <= 19 then
titleText.Text = "Flyweight"
titleText.TextColor3 = Color3.fromRGB(0, 191, 255)
elseif level.Value >= 20 and level.Value <= 34 then
titleText.Text = "Bantamweight"
titleText.TextColor3 = Color3.fromRGB(50, 205, 50)
elseif level.Value >= 35 and level.Value <= 49 then
titleText.Text = "Featherweight"
titleText.TextColor3 = Color3.fromRGB(255, 165, 0)
elseif level.Value >= 50 and level.Value <= 69 then
titleText.Text = "Lightweight"
titleText.TextColor3 = Color3.fromRGB(255, 215, 0)
elseif level.Value >= 70 and level.Value <= 89 then
titleText.Text = "Welterweight"
titleText.TextColor3 = Color3.fromRGB(255, 140, 0)
elseif level.Value >= 90 and level.Value <= 99 then
titleText.Text = "Middleweight"
titleText.TextColor3 = Color3.fromRGB(255, 69, 0)
elseif level.Value >= 100 and level.Value <= 129 then
titleText.Text = "Cruiserweight"
titleText.TextColor3 = Color3.fromRGB(220, 20, 60)
elseif level.Value >= 130 and level.Value <= 149 then
titleText.Text = "Heavyweight"
titleText.TextColor3 = Color3.fromRGB(128, 0, 0)
elseif level.Value == 150 then
titleText.Text = "Super Heavyweight"
titleText.TextColor3 = Color3.fromRGB(75, 0, 130)
end
level.Changed:Connect(function()
levelText.Text = "Lvl " .. tostring(level.Value)
if level.Value >= 1 and level.Value <= 9 then
titleText.Text = "Light Flyweight"
titleText.TextColor3 = Color3.fromRGB(173, 216, 230)
elseif level.Value >= 10 and level.Value <= 19 then
titleText.Text = "Flyweight"
titleText.TextColor3 = Color3.fromRGB(0, 191, 255)
elseif level.Value >= 20 and level.Value <= 34 then
titleText.Text = "Bantamweight"
titleText.TextColor3 = Color3.fromRGB(50, 205, 50)
elseif level.Value >= 35 and level.Value <= 49 then
titleText.Text = "Featherweight"
titleText.TextColor3 = Color3.fromRGB(255, 165, 0)
elseif level.Value >= 50 and level.Value <= 69 then
titleText.Text = "Lightweight"
titleText.TextColor3 = Color3.fromRGB(255, 215, 0)
elseif level.Value >= 70 and level.Value <= 89 then
titleText.Text = "Welterweight"
titleText.TextColor3 = Color3.fromRGB(255, 140, 0)
elseif level.Value >= 90 and level.Value <= 99 then
titleText.Text = "Middleweight"
titleText.TextColor3 = Color3.fromRGB(255, 69, 0)
elseif level.Value >= 100 and level.Value <= 129 then
titleText.Text = "Cruiserweight"
titleText.TextColor3 = Color3.fromRGB(220, 20, 60)
elseif level.Value >= 130 and level.Value <= 149 then
titleText.Text = "Heavyweight"
titleText.TextColor3 = Color3.fromRGB(128, 0, 0)
elseif level.Value == 150 then
titleText.Text = "Super Heavyweight"
titleText.TextColor3 = Color3.fromRGB(75, 0, 130)
end
player:LoadCharacter()
end)
Xp.Changed:Connect(function()
while Xp.Value >= calculateXpRequired(level.Value) do
Xp.Value = Xp.Value - calculateXpRequired(level.Value)
level.Value = level.Value + 1
end
end)
end)