Issue with leveling script

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)

Can someone try to help me with this problem.

You gotta move everything to 1 table, also use SetAsync and not Get

local NumbersForSaving = {saveValue1.Value, saveValue2.Value, saveValue3.Value}`
DS:SetAsync(key, NumbersForSaving)

I am a bit confused, where do I put this code?

You replace what I quoted from your script with what I gave you

Oh, sorry about that question. I am on my laptop and did not see the quote.

1 Like

I replaced my part with your part of the script, but I still have the same problem. I keep going up 1 level and most of my exp went down.

What is exactly the problem? I’m not sure what you need help with?

Not sure if this is what you were trying to fix but I put your two scripts in one. Tell me how it works out for you.

Edit: Forgot to mention, previous data may be forgotten due to the data that’s being stored changing into a table, hope you don’t mind about that one.

-- Script
local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("Test22")

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

	local leaderstatData

	local success, errorMessage = pcall(function()
		leaderstatData = myDataStore:GetAsync("user_" .. player.UserId)
	end)

	if success and leaderstatData then
		player.leaderstats.Level.Value = leaderstatData.Level
		player.leaderstats.Exp.Value = leaderstatData.Exp
		player.RequiredExp.Value = leaderstatData.RequiredExp
	end

	Exp:GetPropertyChangedSignal("Value"):Connect(function()
		if Exp.Value >= RequiredExp.Value then
			Exp.Value = Exp.Value - RequiredExp.Value

			Level.Value += 1
			RequiredExp.Value = Level.Value * 100
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local leaderstatData = {
		Level = player.leaderstats.Level.Value,
		Exp = player.leaderstats.Exp.Value,
		RequiredExp = player.RequiredExp.Value
	}

	local success, errorMessage = pcall(function()
		myDataStore:SetAsync("user_" .. player.UserId, leaderstatData)
	end)

	if not success then
		warn("Error saving data: " .. errorMessage)
	end
end)
1 Like