When I get 50 EXP my level goes back to 0 for some reason
local dataStoreService = game:GetService("DataStoreService")
local LBandLSdata = dataStoreService:GetDataStore("LeaderboardANDLeaderstats")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local leaderboard = Instance.new("Folder")
leaderboard.Name = "leaderboard"
leaderboard.Parent = Player
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 = leaderboard
local RequiredEXP = Instance.new("NumberValue")
RequiredEXP.Name = "RequiredExp"
RequiredEXP.Value = Level.Value * 100
RequiredEXP.Parent = Player
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Value = 0
Wins.Parent = leaderstats
Exp.Changed:Connect(function(Changed)
if Exp.Value >= RequiredEXP.Value then
Exp.Value = 0
Level.Value += 1
RequiredEXP.Value = Level.Value * 100
end
local playerUserId = "Player_"..Player.UserId
local statsData = {
Level = Player.leaderstats.Level.Value or 1;
Exp = Player.leaderboard.Exp.Value;
RequiredEXP = Player.RequiredExp.Value;
Wins = Player.leaderstats.Wins.Value
}
local success, errormessage = pcall(function()
statsData = LBandLSdata:GetAsync(playerUserId)
end)
Level.Value = statsData
if success then
if statsData then
Level.Value = statsData.Level
Exp.Value = statsData.Exp
RequiredEXP.Value = statsData.RequiredEXP
Wins.Value = statsData.Wins
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
local playerUserId = "Player_"..Player.UserId
local statsData = {
Level = Player.leaderstats.Level.Value or 1;
Exp = Player.leaderboard.Exp.Value;
RequiredEXP = Player.RequiredExp.Value;
Wins = Player.leaderstats.Wins.Value
}
local succesRV, errormessageRV = pcall(function()
LBandLSdata:SetAsync(playerUserId, statsData)
end)
if succesRV then
print("data save cool")
else
print("no saved")
warn(errormessageRV)
end
end)
And this is the Part that gives Exp (when i touch the part it gets my level back to 0 from 1
local db = true
local remote = game.ReplicatedStorage.XPGIVE
--Make this the remote event
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and db then
db = false
plr.leaderstats.Wins.Value += 1
plr.leaderboard.Exp.Value += 50
remote:FireClient(plr, script.Parent)
wait(0.5)
db = true --Make sure other players can get it
end
end)