Level up system not working

so i have posted other post with a free script but everyone didnt liked.
then i picked up the custom datastore and added MY LEVEL UP SYSTEM
but didnt worked

here goes the script:

local datastore = game:GetService("DataStoreService"):GetDataStore("GameStats")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(Plr)
	
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Plr
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = leaderstats
	
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Value = 0
	XP.Parent = leaderstats
	
	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Value = "Genin"
	Rank.Parent = leaderstats
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 10
	Coins.Parent = leaderstats
	
	local FC = Instance.new("IntValue")
	FC.Name = "FC"
	FC.Value = 100
	FC.Parent = leaderstats
	
	local key = "user-" .. Plr.userId
	
	
	local storeditems = datastore:GetAsync(key)
			
	if storeditems then
		Level.Value = storeditems[1]
		Rank.Value = storeditems[2]
		XP.Value = storeditems[3]
		Coins.Value = storeditems[4]
		FC.Value = storeditems[5]
	else
		local items = {Level.Value, Rank.Value, XP.Value, FC.Value, Coins.Value} --Change Points or Wins if you changed line 10 or 14
		datastore:SetAsync(key, items)
	end
	
end)

game.ReplicatedStorage.AddXP.OnServerEvent:Connect(function(plr)
	plr.leaderstats.XP.Value = plr.leaderstats.XP.Value + 50
end)

game.Players.PlayerAdded:Connect(function(plr)
	wait(.1)
	local XP = plr.leaderstats.XP
	local Level = plr.leaderstats.Level
	
	while wait() do
		wait(.01)
		if XP.Value >= (100 * (Level.Value + 1)) then
			Level.Value = Level.Value + 1
			XP.Value = 0
			game.ReplicatedStorage.LevelUpGui:FireClient(plr)
		end
	end
end)









game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Level.Value, player.leaderstats.Rank.Value, player.leaderstats.XP.Value, player.leaderstats.Coins.Value, player.leaderstats.FC.Value} --Change Points or Wins if you changed line 10 or 14
	local key = "user-" .. player.userId
	
	datastore:SetAsync(key, items)
end)
1 Like

Your second PlayerAdded Connection is most probably indexing the XP and Level before they’re made. Why not just handle it where they’re created?

Additionally, you should try to use the Changed event rather than a inefficient while loop.

1 Like

Like this?

local datastore = game:GetService("DataStoreService"):GetDataStore("GameStats")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(Plr)
	
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Plr
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = leaderstats
	
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Value = 0
	XP.Parent = leaderstats
	
	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Value = "Genin"
	Rank.Parent = leaderstats
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 10
	Coins.Parent = leaderstats
	
	local FC = Instance.new("IntValue")
	FC.Name = "FC"
	FC.Value = 100
	FC.Parent = leaderstats
	
	local key = "user-" .. Plr.userId
	
	
	local storeditems = datastore:GetAsync(key)
			
	if storeditems then
		Level.Value = storeditems[1]
		Rank.Value = storeditems[2]
		XP.Value = storeditems[3]
		Coins.Value = storeditems[4]
		FC.Value = storeditems[5]
	else
		local items = {Level.Value, Rank.Value, XP.Value, FC.Value, Coins.Value} --Change Points or Wins if you changed line 10 or 14
		datastore:SetAsync(key, items)
	end
	
    while wait() do
		wait(.01)
		if XP.Value >= (100 * (Level.Value + 1)) then
			Level.Value = Level.Value + 1
			XP.Value = 0
			game.ReplicatedStorage.LevelUpGui:FireClient(Plr)
		end
	end	
end)

game.ReplicatedStorage.AddXP.OnServerEvent:Connect(function(plr)
	plr.leaderstats.XP.Value = plr.leaderstats.XP.Value + 50
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Level.Value, player.leaderstats.Rank.Value, player.leaderstats.XP.Value, player.leaderstats.Coins.Value, player.leaderstats.FC.Value} --Change Points or Wins if you changed line 10 or 14
	local key = "user-" .. player.userId
	
	datastore:SetAsync(key, items)
end)
2 Likes