Levelsystem Datastore Adding extra Level and EXP when player rejoins

When a player beets and obby and gains EXP and leave and rejoin they end up with another Level and some EXP and the MAX EXP doesn’t change…
Code–Level/EXP/MAXEXP datastore

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_2"..plr.userId
	local save1 = plr.Level
	local save2 = plr.Level.Current
	local save3 = plr.Level.Max

	local GetSaved = ds:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
		save3.Value = GetSaved[3]
	else
		local NumberForSaving = {save1.Value, save2.Value, save3.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_2"..plr.userId, {plr.Level.Value, plr.Level.Current.Value, plr.Level.Max.Value})
end)

Before Player leaves –

After Player Rejoins Game–

You notice they gain an extra level and there EXP is less then before and the Coins and Wins in the leaderboard are the same as they should be… Anybody know how to fix this I assume its something in PlayerAdded but I don’t wanna mess up the Datastore??!

Its basically just supposed to be the same when the player joins again like all data when a player comes back!

1 Like
game.Players.PlayerAdded:Connect(function(player)
	local level = Instance.new("IntValue", player)
	level.Name = "Level"
	level.Value = 1

	local exp = Instance.new("IntValue", level)
	exp.Name = "Current"
	exp.Value = 0

	local maxExp = Instance.new("IntValue", level)
	maxExp.Name = "Max"
	maxExp.Value = 100
	
	
	exp.Changed:Connect(function(val)
		if exp.Value >= maxExp.Value then
			--do somethin
			
			level.Value = level.Value + 1
			exp.Value = exp.Value - maxExp.Value
			maxExp.Value = maxExp.Value * 1.25
		end
	end)
end)
``` Could this be the issue this is the LevelSystem script I prob have to move this bottom chunk out

I suggest you just put it all in one script.

local DataStore = game:GetService("DataStoreService"):GetDataStore("SaveData") --Name the DataStore whatever you want

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

    local level = Instance.new("IntValue", player)
    level.Name = "Level"
    level.Value = 1

	local exp = Instance.new('NumberValue', level)
	exp.Name = "Current"
	exp.Value = 0

    local maxExp = Instance.new("IntValue", level)
    maxExp.Name = "Max"
    maxExp.Value = 100

		if exp.Value >= maxExp.Value then
			--do somethin
			
			level.Value = level.Value + 1
			exp.Value = exp.Value - maxExp.Value
			maxExp.Value = maxExp.Value * 1.25
		end
	end)

	local value1Data = level
    local value2Data = exp
    local value3Data = maxExp

	local s, e = pcall(function()
		value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
        value2Data = DataStore:GetAsync(player.UserId.."-Value2") or 0
        value3Data = DataStore:GetAsync(player.UserId.."-Value3") or 0
	end)

	if s then
		level.Value = value1Data --setting data if its success
        exp.Value = value2Data
        maxExp.Value = value3Data
	else
		game:GetService("TestService"):Error(e)  --if not success then we error it to the console
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
	DataStore:SetAsync(player.UserId.."-Value1", player.level.Value) --setting data
    DataStore:SetAsync(player.UserId.."-Value2", player.exp.Value)
    DataStore:SetAsync(player.UserId.."-Value3", player.maxExp.Value)
	end)
	if not s then game:GetService("TestService"):Error(e) 
	end
end)