Is there anything wrong?

The scripts problem is the levelXP. I don’t know how to explain this but the error shows this.

Could you please show the lines of codes that seemed to have errored?

ErrorScript

Here is the whole script:

local DataStoreService = game:GetService("DataStoreService") -- Gets DataStoreService

local playerData = DataStoreService:GetDataStore("PlayerData") -- To work, API Services has to be on





local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player



	local Cash = Instance.new("IntValue") --Sets up value for leaderstats

	Cash.Name = "Cash"

	Cash.Parent = leaderstats
	
	
	
	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Parent = leaderstats
	
	
	
	local levelXP = game.ReplicatedStorage.levelXP	

	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		-- Data exists for this player

		Cash.Value = data
		Rank.Value = data
		levelXP.Value = data

	else

		-- Data store is working, but no current data for this player

		Cash.Value = 0
		Rank.Value = "Guest"
		levelXP.XP = 0
	end

end



local function onPlayerExit(player)  --Runs when players exit



	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player.leaderstats.Cash.Value, player.leaderstats.Rank.Value, game.ReplicatedStorage.levelXP.Value) --Saves player data

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

I don’t want levelXP on the leaderstats but on a gui showing how much XP the player has left to get to the next level.

Code works fine for me, levelXP isn’t showing up in my leaderstats.

Did you make an edit on levelXP = data?
Was it levelXP.Value == data and you fixed it?
If not please provide some more information and another picture of the console if possible, I’m getting different results.

What was your results in the console?

Nothing, clear, when I left the game it said it could not save my data.

Make sure to fix this
image
on line 53, make the levelXP.XP to levelXP.Value

Also enable API if you haven’t already, you probably have.

That’s not right. The error says the problem is on line 1 of PlayerRank.LocalScript. The highlighted line clearly isn’t line 1.
Oh my bad, there are two errors listed.

It’s a local script, the script he is previewing is a server script.

1 Like

You cannot SetAsync multiple values, you should use a table to store every value and then SetAsync the table. Btw you should use :BindToClose() to secure data save after the server is closed.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")


local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats

	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Parent = leaderstats

	local levelXP = game.ReplicatedStorage.levelXP	
	local playerUserId = "Player_" .. player.UserId
	local data = playerData:GetAsync(playerUserId)

	if data then
		Cash.Value = data[1] or 0
		Rank.Value = data[2] or "Guest"
		levelXP.Value = data[3] or 0
	end
end



local function onPlayerExit(player)
	local Values = {
		player.leaderstats.Cash.Value;
		player.leaderstats.Rank.Value;
		game.ReplicatedStorage.levelXP.Value
	}


	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, Values)
	end)



	if not success then
		warn('Could not save data!')
	end

end

local function onServerClose()
	for i, player in pairs(Players:GetPlayers()) do
		local Values = {
			player.leaderstats.Cash.Value;
			player.leaderstats.Rank.Value;
			game.ReplicatedStorage.levelXP.Value
		}


		local success, err = pcall(function()
			local playerUserId = "Player_" .. player.UserId

			playerData:SetAsync(playerUserId, Values)
		end)



		if not success then
			warn('Could not save data!')
		end
	end
end


Players.PlayerAdded:Connect(onPlayerJoin)

Players.PlayerRemoving:Connect(onPlayerExit)

game:BindToClose(onServerClose)

I have another script but I don’t think I am allowed to get help here since this post is for another script so I am just going to make another post

local DataStoreService = game:GetService("DataStoreService") -- Gets DataStoreService

local playerData = DataStoreService:GetDataStore("PlayerData") -- To work, API Services has to be on





local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player



	local Cash = Instance.new("IntValue") --Sets up value for leaderstats

	Cash.Name = "Cash"

	Cash.Parent = leaderstats



	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Parent = leaderstats



	local levelXP = game.ReplicatedStorage.levelXP	

	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		-- Data exists for this player

		Cash.Value = data
		Rank.Value = data
		levelXP.Value = data

	else

		-- Data store is working, but no current data for this player

		Cash.Value = 0
		Rank.Value = "Guest"
		levelXP.Value = 0
	end

end



local function onPlayerExit(player)  --Runs when players exit



	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player.leaderstats.Cash.Value, player.leaderstats.Rank.Value, game.ReplicatedStorage.levelXP.Value) --Saves player data

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

The script this way works as it’s intended for me, I’m not sure where you get the problem, could you run the game again and take a picture of the console once again?