DataStore Make My Level System Broken

Hi Im CTG And I Need Help With My Level System

Here Is The Bug:

5ee6333ea6a6fc96f71788ae9f3b9a0b

And Here Is The Code:

-- Var
local datastore = game:GetService("DataStoreService")
local LevelDataStore = datastore:GetDataStore("LevelDataStore")
local ExpDataStore = datastore:GetDataStore("ExpDataStore")
local MaxExpDataStore = datastore:GetDataStore("MaxExpDataStore")
local MoneyDataStore = datastore:GetDataStore("MoneyDataStore")
local players = game:GetService("Players")

-- PlayerAdd
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 = "Exp"
	exp.Value = 0

	local maxExp = Instance.new("IntValue", level)
	maxExp.Name = "Max"
	maxExp.Value = 50
	
	local Money = Instance.new("IntValue", player)
	Money.Name = "Money"
	
	local data 
	local data2 
	local data3 
	local data4 
	
	local success, errormessage = pcall(function()
		data = LevelDataStore:GetAsync(player.UserId.."-Level")
		data2 = ExpDataStore:GetAsync(player.UserId.."-Exp")
		data3 = MaxExpDataStore:GetAsync(player.UserId.."-MaxExp")
		data4 = MoneyDataStore:GetAsync(player.UserId.."-Money")
	end)
	
	if success then
		level.Value = data
	else
		print(player.Name.. "SaveData Fail")
	end
	
	if success ~= nil then
		exp.Value = data2
	else
		print(player.Name.. "SaveData Fail")
	end
	
	if success then
		maxExp.Value = data
	else
		print(player.Name.. "SaveData Fail")
	end
	
	if success then
		Money.Value = data
	else
		print(player.Name.. "SaveData Fail")
	end
	
	-- Player Leave
	game.Players.PlayerRemoving:Connect(function(player)
		
		local success, errormessage = pcall(function()
			data = LevelDataStore:GetAsync(player.UserId.."-Level",player.Level.Value)
			data2 = ExpDataStore:GetAsync(player.UserId.."-Exp",player.Level.Exp.Value)
			data3 = MaxExpDataStore:GetAsync(player.UserId.."-MaxExp",player.Level.Max.Value)
			data4 = MoneyDataStore:GetAsync(player.UserId.."-Money",player.Money.Value)
		end)
		
		if success then
			print(player.Name.." Has SaveData")
		else
			print(player.Name.. "SaveData Fail")
			warn(errormessage)
		end
	end)
	
	exp.Changed:Connect(function(changed)
		if exp.Value >= maxExp.Value then
			exp.Value = exp.Value - maxExp.Value
			
			level.Value += 1
			maxExp.Value = maxExp.Value*1.1
		end
	end)
end)

Pls Help Me Bc Im Kinda Bad In Data

2 Likes

Are there any errors in your output? Also, I’ve noticed that when you try to set player data in the PlayerRemoving event, you’re using GetAsync instead of SetAsync, which you need to change in-order for the data to save. (GetAsync returns the current value; it doesn’t set it)

Hey! Try this I guess?

-- Var
local datastore = game:GetService("DataStoreService")
local LevelDataStore = datastore:GetDataStore("LevelDataStore")
local ExpDataStore = datastore:GetDataStore("ExpDataStore")
local MaxExpDataStore = datastore:GetDataStore("MaxExpDataStore")
local MoneyDataStore = datastore:GetDataStore("MoneyDataStore")
local players = game:GetService("Players")

-- PlayerAdd
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 = "Exp"
	exp.Value = 0

	local maxExp = Instance.new("IntValue", level)
	maxExp.Name = "Max"
	maxExp.Value = 50
	
	local Money = Instance.new("IntValue", player)
	Money.Name = "Money"
	
	local data 
	local data2 
	local data3 
	local data4 
	
	local success, errormessage = pcall(function()
		data = LevelDataStore:GetAsync(player.UserId.."-Level")
		data2 = ExpDataStore:GetAsync(player.UserId.."-Exp")
		data3 = MaxExpDataStore:GetAsync(player.UserId.."-MaxExp")
		data4 = MoneyDataStore:GetAsync(player.UserId.."-Money")
	end)
	
	if success then
		level.Value = data
	else
		print(player.Name.. "SaveData Fail")
	end
	
	if success ~= nil then
		exp.Value = data2
	else
		print(player.Name.. "SaveData Fail")
	end
	
	if success then
		maxExp.Value = data3
	else
		print(player.Name.. "SaveData Fail")
	end
	
	if success then
		Money.Value = data4
	else
		print(player.Name.. "SaveData Fail")
	end
end)

for _, v in pairs(players:GetPlayers()) do
    v:WaitForChild("Exp"):GetPropertyChangedSignal("Value"):Connect(function(changedVal)
        if changedVal >= v:WaitForChild("Max").Value then
			v:WaitForChild("Exp").Value = (v:WaitForChild("Exp").Value - v:WaitForChild("Max").Value)
			
			v:WaitForChild("Level").Value += 1
			v:WaitForChild("Max").Value = (maxExp.Value * 1.1) -- 50 * 1.1 = 55; 55 * 1.1 = 60.5
		end
    end)
end

game.Players.PlayerRemoving:Connect(function(player)
		
    local success, errormessage = pcall(function()
        data = LevelDataStore:SetAsync(player.UserId.."-Level",player.Level.Value)
        data2 = ExpDataStore:SetAsync(player.UserId.."-Exp",player.Level.Exp.Value)
        data3 = MaxExpDataStore:SetAsync(player.UserId.."-MaxExp",player.Level.Max.Value)
        data4 = MoneyDataStore:SetAsync(player.UserId.."-Money",player.Money.Value)
    end)
    
    if success then
        print(player.Name.." Has SaveData")
    else
        print(player.Name.. "SaveData Fail")
        warn(errormessage)
    end
end)

it not have any error it just make my lvl like that

ok wait ima try it also i scared it by im write data bad lol

1 Like

Don’t worry haha, it’s all good!

It Even Not Appear Let Me Check

I recommend rewrite the data script as this just messes up

But Im Test Already And The Data Work It Just Make My Lvl System Broken Maybe U Can Find Bug In Lvl System

U Scope Wrong And Make Whole Script Broken But Im Fix It And Still Not Work

Put the for loop inside the the PlayerAdded and replace the for loop to while loop.

i know you didn’t made the thread, just addition to your script

1 Like

im fix he script rn let me test it

-- Var
local datastore = game:GetService("DataStoreService")
local LevelDataStore = datastore:GetDataStore("LevelDataStore")
local ExpDataStore = datastore:GetDataStore("ExpDataStore")
local MaxExpDataStore = datastore:GetDataStore("MaxExpDataStore")
local MoneyDataStore = datastore:GetDataStore("MoneyDataStore")
local players = game:GetService("Players")

-- PlayerAdd
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 = "Exp"
	exp.Value = 0

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

	local Money = Instance.new("IntValue", player)
	Money.Name = "Money"

	local data 
	local data2 
	local data3 
	local data4 

	local success, errormessage = pcall(function()
		data = LevelDataStore:GetAsync(player.UserId.."-Level")
		data2 = ExpDataStore:GetAsync(player.UserId.."-Exp")
		data3 = MaxExpDataStore:GetAsync(player.UserId.."-MaxExp")
		data4 = MoneyDataStore:GetAsync(player.UserId.."-Money")
	end)

	if success then
		level.Value = data
	else
		print(player.Name.. "SaveData Fail")
	end

	if success ~= nil then
		exp.Value = data2
	else
		print(player.Name.. "SaveData Fail")
	end
	
	if success then
		maxExp.Value = data3
	else
		print(player.Name.. "SaveData Fail")
	end

	if success then
		Money.Value = data4
	else
		print(player.Name.. "SaveData Fail")
	end
	
	while wait() do
		player:WaitForChild("Exp"):GetPropertyChangedSignal("Value"):Connect(function(changedVal)
			if changedVal >= player:WaitForChild("Max").Value then
				player:WaitForChild("Exp").Value = (player:WaitForChild("Exp").Value - player:WaitForChild("Max").Value)

				player:WaitForChild("Level").Value += 1
				player:WaitForChild("Max").Value = (maxExp.Value * 1.1) 
				
				game.Players.PlayerRemoving:Connect(function(player)

					local success, errormessage = pcall(function()
						data = LevelDataStore:SetAsync(player.UserId.."-Level",player.Level.Value)
						data2 = ExpDataStore:SetAsync(player.UserId.."-Exp",player.Level.Exp.Value)
						data3 = MaxExpDataStore:SetAsync(player.UserId.."-MaxExp",player.Level.Max.Value)
						data4 = MoneyDataStore:SetAsync(player.UserId.."-Money",player.Money.Value)
					end)

					if success then
						print(player.Name.." Has SaveData")
					else
						print(player.Name.. "SaveData Fail")
						warn(errormessage)
					end
				end)
			end
		end)
	end
end)

Idk im right but still not work

The script on the loop must look like this:

while wait() do
exp.Changed:Connect(function() --- just "Changed" function? There's no ChangeInProoerty function for Values, because the only thing that could change is the Value
If changedVal>=Max.Value then
Exp.Value -= Max.Value
Level.Value += 1
Max.Value = maxExp.Value*1.1
end
end

Correct me in some var because im on phone and it auto capitalize, sorry.

You don’t need to put the PlayerRemoving inside the while loop

it still not work

-- Var
local datastore = game:GetService("DataStoreService")
local LevelDataStore = datastore:GetDataStore("LevelDataStore")
local ExpDataStore = datastore:GetDataStore("ExpDataStore")
local MaxExpDataStore = datastore:GetDataStore("MaxExpDataStore")
local MoneyDataStore = datastore:GetDataStore("MoneyDataStore")
local players = game:GetService("Players")

-- PlayerAdd
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 = "Exp"
	exp.Value = 0

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

	local Money = Instance.new("IntValue", player)
	Money.Name = "Money"

	local data 
	local data2 
	local data3 
	local data4 

	local success, errormessage = pcall(function()
		data = LevelDataStore:GetAsync(player.UserId.."-Level")
		data2 = ExpDataStore:GetAsync(player.UserId.."-Exp")
		data3 = MaxExpDataStore:GetAsync(player.UserId.."-MaxExp")
		data4 = MoneyDataStore:GetAsync(player.UserId.."-Money")
	end)

	if success then
		level.Value = data
	else
		print(player.Name.. "SaveData Fail")
	end

	if success ~= nil then
		exp.Value = data2
	else
		print(player.Name.. "SaveData Fail")
	end
	
	if success then
		maxExp.Value = data3
	else
		print(player.Name.. "SaveData Fail")
	end

	if success then
		Money.Value = data4
	else
		print(player.Name.. "SaveData Fail")
	end
	
	while wait() do
		exp.Changed:Connect(function()
			if exp.Value >= maxExp.Value then
				exp.Value -= maxExp.Value
				level.Value += 1
				maxExp.Value = maxExp.Value*1.1
			end
		end)
	end

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

	local success, errormessage = pcall(function()
		data = LevelDataStore:SetAsync(player.UserId.."-Level",player.Level.Value)
		data2 = ExpDataStore:SetAsync(player.UserId.."-Exp",player.Level.Exp.Value)
		data3 = MaxExpDataStore:SetAsync(player.UserId.."-MaxExp",player.Level.Max.Value)
		data4 = MoneyDataStore:SetAsync(player.UserId.."-Money",player.Money.Value)
	end)

	if success then
		print(player.Name.." Has SaveData")
	else
		print(player.Name.. "SaveData Fail")
			warn(errormessage)
		end
	end)
end)
1 Like

You don’t put the PlayerRemoving inside the PlayerAdded

1 Like

You’re doing a bit wrong. You’re already hearing for changes from the .Changed event. You can just put the exp.Changed. The while loop isn’t needed.

2 Likes

By the way, was any data saved to these datastores?

1 Like

it save just make lvl broken lol

1 Like