I am trying to make a leveling system, but I encountered some issues.
When I rejoin the game after collecting exp, It lowers how much exp I have and increases my level by 1. I think I figured out why its doing it, but I can’t figure out how to fix it. It thinks it needs 100 exp to get next level.
Example: When I join rejoin the game with 230 exp, it takes the 200 exp and gives me 2 levels, but I still have 30 exp leftover. Video Link
I could not figure out how to create my own level script so, I tried using level tutorials on Youtube, but they always had at least 1 problem with the script. I just decided to ask for help instead of trying to find a script that works.
These scripts are the Level and Database:
game.Players.PlayerAdded:Connect(function(Player)
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 * 100
Exp.Changed:Connect(function(Changed)
if Exp.Value >= RequiredExp.Value then
Exp.Value = Exp.Value - RequiredExp.Value
Level.Value += 1
RequiredExp.Value = Level.Value * 100
end
end)
end)
local ds = game:GetService("DataStoreService"):GetDataStore("Test22")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local key = "user_"..plr.UserId
local saveValue1 = plr.leaderstats.Level
local saveValue2 = plr.leaderstats.Exp
local saveValue3 = plr.RequiredExp
local getSaved = ds:GetAsync(key)
if getSaved then
saveValue1.Value = getSaved[1]
saveValue2.Value = getSaved[2]
saveValue3.Value = getSaved[3]
else
local NumbersForSaving = {saveValue1.Value}
local NumbersForSaving2 = {saveValue2.Value}
local NumbersForSaving3 = {saveValue3.Value}
ds:GetAsync(key, NumbersForSaving, NumbersForSaving2, NumbersForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local saveValue1 = player.leaderstats.Level
local saveValue2 = player.leaderstats.Exp
local saveValue3 = player.RequiredExp
ds:SetAsync("user_"..player.UserId, {saveValue1.Value, saveValue2.Value, saveValue3.Value})
end)