Basically when you gain a level, it goes from whatever level you've just gotten back to zero (level 1 -> level 2 (1 second-ish later) -> level 0) heres the code, its messy ;-;.
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local exp = Instance.new("StringValue", player)
exp.Name = "exp"
local Level = Instance.new("IntValue", exp)
Level.Name = "Level"
Level.Value = 1
local Exp = Instance.new("IntValue", exp)
Exp.Name = "Exp"
Exp.Value = 0
local RequiredExp = Instance.new("IntValue", player)
RequiredExp.Name = "RequiredExp"
RequiredExp.Value = Level.Value * 125
--level up and xp down here
Exp.Changed:Connect(function(Changed)
if Exp.Value >= RequiredExp.Value then
Exp.Value = Exp.Value - RequiredExp.Value
Level.Value += 1
RequiredExp.Value = Level.Value * 125
local expdata
local leveldata
local success, errormessage = pcall(function()
expdata = myDataStore:GetAsync(player.UserId.."-Exp")
leveldata = myDataStore:GetAsync(player.UserId.."-Level")
end)
if success then
Exp.Value = expdata
Level.Value = leveldata
else
print("error getting your data sorry bruhng")
warn(errormessage)
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-Exp",player.exp.Exp.Value)
myDataStore:SetAsync(player.UserId.."-Level",player.exp.Level.Value)
end)
if success then
print("data saved!")
else
print("there was an error in saving data")
warn (errormessage)
end
end)
end)
I have other scripts which are simple that just give xp when you touch a block (for testing), but this is the main script used.
Let's step through your code for when the player's experience changes :
If the fact that the player's level up information is being reverted is a bug, then don't overwrite the current level information with the previously saved data.