Hello. I am wondering how to save these NumberValues. It is for a level up and exp system. With the way I have tried, the bar breaks and it makes Level and Exp the same value. Please help!
This first script is a 'leaderstats' script in ServerScriptService
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(Player)
-- Leaderstats --
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local Level = Instance.new("NumberValue", leaderstats)
Level.Name = "Level"
Level.Value = 1
local Exp = Instance.new("NumberValue", leaderstats)
Exp.Name = "Exp"
Exp.Value = 0
local RequiredExp = Instance.new("NumberValue", Player)
RequiredExp.Name = "RequiredExp"
RequiredExp.Value = Level.Value * 1000
local playerUserId = "Player_"..Player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
Level.Value = data
Exp.Value = data
else
Level.Value = 1
Exp.Value = 1
end
--Level Up & Exp--
Exp.Changed:Connect(function(Changed)
if Exp.Value >= RequiredExp.Value then
Exp.Value = Exp.Value - RequiredExp.Value
Level.Value += 1
RequiredExp.Value = Level.Value * 1000
end
end)
end)
local function onPlayerExit(Player)
local success, err = pcall(function()
local playerUserId = "Player_"..Player.UserId
playerData:SetAsync(playerUserId, Player.leaderstats.Exp.Value)
playerData:SetAsync(playerUserId, Player.leaderstats.Level.Value)
end)
if not success then
warn("Could not save data")
end
end
game.Players.PlayerRemoving:Connect(onPlayerExit)
This second script is in 'BackgroundFrame'.
local Player = game.Players.LocalPlayer
local Exp = Player.leaderstats.Exp
local RequiredExp = Player.RequiredExp
Exp.Changed:Connect(function(Changed)
if Changed then
script.Parent.ExpBar:TweenSize(UDim2.new(Exp.Value / RequiredExp.Value, 0, 1, 0))
end
end)
while wait() do
script.Parent.ExpLabel.Text = Exp.Value.." / "..RequiredExp.Value
end
The final script is in 'LevelLabel'.
local Player = game.Players.LocalPlayer
while wait() do
script.Parent.Text = "Level : "..Player.leaderstats.Level.Value
end
Level Label displays the level. Example = Level : 1
Exp Label displays how much Exp you have. Example = 10 / 1000
Exp Bar is a bar that goes up depending on how much exp you have. For example, if you have 500 / 1000 exp the bar would be at half.

2 comments
you are saving 1 thing and loading 1 thing, setting both the exp and level to 1 thing. save the data in a table.
and how do i do that