Level keeps dropping back to 0

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)
1 Like

For some reason this code works even though its basically the same so perhaps it has to do with the datastore?

I tried putting “NumberValue” in the main script like it says in this one but still no difference.

game.Players.PlayerAdded:Connect(function(Player)

	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"
	
	local leaderboard = Instance.new("Folder", Player)
	leaderboard.Name = "leaderboard"
	
	
	local Level = Instance.new("NumberValue", leaderstats)
	Level.Name = "Level"
	Level.Value = 1
	
	local Exp = Instance.new("NumberValue", leaderboard)
	Exp.Name = "Exp"
	Exp.Value = 0
	
	local RequiredEXP = Instance.new("NumberValue", Player)
	RequiredEXP.Name = "RequiredExp"
	RequiredEXP.Value = Level.Value * 100
	
	local Wins = Instance.new("NumberValue", leaderstats)
	Wins.Name = "Wins"
	Wins.Value = 0

	--  LEVELS. XP .REQUIRED
	
	Exp.Changed:Connect(function(Changed)
		
		if Exp.Value >= RequiredEXP.Value then
			Exp.Value = 0
			
			Level.Value += 1
			RequiredEXP.Value = Level.Value * 100
		end
	end)
end)

I don’t think you need the RequiredExp as that can be calculated each time the Exp changes. Example:

local RequiredExp = Level.Value * 100

No need to create a Number Value.