I am trying to create a level GUI. I already have the GUI available however I am trying to make the bar move up and display the correct amount of exp a user has and the correct level.
The Level and EXP are stored in the leaderboard, which I’m not sure if it was the most efficient way to do it but thats what I currently have setup. I am trying to get the information from the leaderstats to update the gui accordinly but what I have been trying by using information from videos or the AI assistant hasn’t helped.
The GUI kind of just stays the default and doesn’t change I have posted a picture for reference of what my level is and the exp that i have accumulated.
GUI:
Scripts:
Script in GUI
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SavePoints = ReplicatedStorage:WaitForChild("SavePoints") -- Use the existing SavePoints RemoteEvent
local player = game.Players.LocalPlayer
local gui = script.Parent
-- Show loading state while data is fetched
local function showLoadingState()
local infoFrame = gui.Frame.Info
infoFrame.ProgressBar.Amount.Text = "Loading..."
infoFrame.LevelFrame.CurrentLevel.Text = "Loading..."
end
-- Update the Level GUI with current stats
local function updateLevelGUI(currentEXP, xpForNextLevel, currentLevel)
local infoFrame = gui.Frame.Info
local progressBar = infoFrame.ProgressBar
local levelText = infoFrame.LevelFrame.CurrentLevel
local playerText = infoFrame.LevelFrame.PlayerName
local playerAvatarImage = gui.Frame.Player.Icon
local amountText = progressBar.Amount
-- Calculate progress as a percentage
local progress = currentEXP / xpForNextLevel
progressBar.Progress.Size = UDim2.new(progress, 0, 1, 0)
-- Update the XP progress text or show "MAX" for level cap
if currentLevel >= 100 then
amountText.Text = "MAX"
else
amountText.Text = tostring(currentEXP) .. " / " .. tostring(xpForNextLevel)
end
-- Update level and player information
levelText.Text = "Level: " .. tostring(currentLevel)
playerText.Text = player.Name
-- Update the player's avatar image
local userId = player.UserId
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
playerAvatarImage.Image = content
end
-- Listen for updates sent via the SavePoints event
SavePoints.OnClientEvent:Connect(function()
-- Fetch leaderboard stats from CoinDataStoreScript
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local currentEXP = leaderstats:FindFirstChild("EXP") and leaderstats.EXP.Value or 0
local currentLevel = leaderstats:FindFirstChild("Level") and leaderstats.Level.Value or 0
local xpForNextLevel = currentLevel * 100 -- Assuming XP needed scales with level, e.g., 100 XP per level
-- Update the GUI with the retrieved stats
showLoadingState()
updateLevelGUI(currentEXP, xpForNextLevel, currentLevel)
end
end)
Leaderboard server script if needed for reference:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local coinsDataStore = DataStoreService:GetDataStore("PlayerCoinsDataStore")
local expLevelDataStore = DataStoreService:GetDataStore("PlayerExpLevelDataStore")
local remoteEvent = ReplicatedStorage:WaitForChild("LeveledUp")
local remoteFunction = ReplicatedStorage:WaitForChild("IncreaseStats")
local savePointsEvent = ReplicatedStorage:WaitForChild("SavePoints") -- Using the correct SavePoints event
-- Function to update points label on the client side
local function updatePointsLabel(player, pointsValue)
remoteEvent:FireClient(player, pointsValue) -- Update points label on the client
end
-- Handle stat increase when the client invokes the remote function
local function increaseStats(player, button)
if player.Points.Value > 0 then -- Ensure player has points to spend
player.Points.Value -= 1
local pointsValue = player.Points.Value
local stat = player.StatsFolder:FindFirstChild(button)
if stat then
stat.Value += 1
local statValue = stat.Value
updatePointsLabel(player, pointsValue) -- Call to update points after stat increase
return pointsValue, statValue
end
end
warn("Invalid stat button or insufficient points for", player.Name)
return player.Points.Value, nil
end
-- Bind the "increaseStats" function to the remote function's "OnServerInvoke" callback
remoteFunction.OnServerInvoke = increaseStats
-- Load player data when they join
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local level = Instance.new("IntValue")
level.Name = "Level"
level.Value = 1
level.Parent = leaderstats
local exp = Instance.new("IntValue")
exp.Name = "EXP"
exp.Value = 0
exp.Parent = leaderstats
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 1
points.Parent = player
local statsFolder = Instance.new("Folder")
statsFolder.Name = "StatsFolder"
statsFolder.Parent = player
local Attack = Instance.new("IntValue")
Attack.Name = "Attack"
Attack.Value = 1
Attack.Parent = statsFolder
local Special = Instance.new("IntValue")
Special.Name = "Special"
Special.Value = 1
Special.Parent = statsFolder
local Defence = Instance.new("IntValue")
Defence.Name = "Defence"
Defence.Value = 1
Defence.Parent = statsFolder
-- Load saved coins from the coins DataStore
local success, coins = pcall(function()
return coinsDataStore:GetAsync(player.UserId)
end)
if success and coins then
cash.Value = coins
else
cash.Value = 0
end
-- Load saved EXP, Level, and Points from the new DataStore
local successExpLevel, savedData = pcall(function()
return expLevelDataStore:GetAsync(player.UserId)
end)
if successExpLevel and savedData then
exp.Value = savedData.EXP or 0
level.Value = savedData.Level or 1
points.Value = savedData.Points or 1 -- Default to 1 if no saved data
-- Load saved stats (Attack, Defence, Special)
local savedStats = savedData.Stats or {}
Attack.Value = savedStats.Attack or 1
Defence.Value = savedStats.Defence or 1
Special.Value = savedStats.Special or 1
else
exp.Value = 0
level.Value = 1
points.Value = 1
end
-- Add RequiredEXP in stats folder (for tracking)
local stats = Instance.new("Folder")
stats.Name = "stats"
stats.Parent = player
local requiredEXP = Instance.new("IntValue")
requiredEXP.Name = "RequiredEXP"
requiredEXP.Value = 500
requiredEXP.Parent = stats
exp.Changed:Connect(function()
if exp.Value >= requiredEXP.Value then
exp.Value -= requiredEXP.Value
level.Value += 1
requiredEXP.Value = 500 + (level.Value - 1) * 1
points.Value += 1
updatePointsLabel(player, points.Value) -- Update points after leveling up
-- Save updated Points, Level, EXP, and Stats to DataStore
local successSavePoints = pcall(function()
expLevelDataStore:SetAsync(player.UserId, {
EXP = exp.Value,
Level = level.Value,
Points = points.Value,
Stats = {
Attack = Attack.Value,
Defence = Defence.Value,
Special = Special.Value
}
})
end)
if not successSavePoints then
warn("Failed to save points for player:", player.Name)
end
end
end)
-- Save player data when they leave
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
coinsDataStore:SetAsync(player.UserId, player.leaderstats.Cash.Value)
end)
if not success then
warn("Failed to save coins for player:", player.Name, errorMessage)
end
local exp = player.leaderstats:FindFirstChild("EXP")
local level = player.leaderstats:FindFirstChild("Level")
local points = player:FindFirstChild("Points")
if exp and level and points then
local successExpLevel, errorMessage = pcall(function()
expLevelDataStore:SetAsync(player.UserId, {
EXP = exp.Value,
Level = level.Value,
Points = points.Value,
Stats = {
Attack = Attack.Value,
Defence = Defence.Value,
Special = Special.Value
}
})
end)
if not successExpLevel then
warn("Failed to save EXP, Level, and Points for player:", player.Name, errorMessage)
end
end
end)
-- Award coins and XP when killing Troll Guard
local function awardPlayer(player, coinsAwarded, expAwarded)
if player and player:FindFirstChild("leaderstats") then
local cash = player.leaderstats.Cash
cash.Value += coinsAwarded
local exp = player.leaderstats.EXP
exp.Value += expAwarded
end
end
-- Mobs behavior for awarding XP and coins
local mobsFolder = game.Workspace:FindFirstChild("Mobs")
if mobsFolder then
for _, mob in pairs(mobsFolder:GetChildren()) do
if mob.Name == "Troll Guard" then
local humanoid = mob:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
local creatorTag = humanoid:FindFirstChild("creator")
if creatorTag and creatorTag.Value then
local player = creatorTag.Value
awardPlayer(player, 0, 50) -- award coins, xp xp is 50 here
end
end)
end
end
end
end
end)