DataStore Make My Level System Broken

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

im fix help he already lol no need to tell he

1 Like

so i can get data rn im kinda bad in data

1 Like

thank when me thinking and i know to fix thank for help me wait me solution you

1 Like