Issue with leaderstats

What is wrong?

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)

All help appreciated

1 Like

Hmmm… Nothing looks wrong to me. Do you have datastores enabled?

1 Like

Oh I forgot to metion. The problem is that my Player Rank TextLabel doesnt work

1 Like

I have no idea then. I have the same issue also sometimes.

1 Like
local Player = game:GetService("Players").LocalPlayer
local PlayerRank = script.Parent.UserFrame.PlayerRank

local leaderstats = Player:WaitForChild("leaderstats")
local Rank = leaderstats:WaitForChild("Rank")

PlayerRank.Text = Rank.Value
1 Like

Is levelXP an IntValue or NumberValue?

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 XP = Instance.new("IntValue")
	XP.Name = "Exp"
	XP.Parent = player --store exp inside player instead of replicatedstorage

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

	if type(data) == "table" then
		Cash.Value = data[1] or 0
		Rank.Value = data[2] or "Guest"
		XP.Value = data[3] or 0
	else
		print("No data to load!")
	end
end

local function onPlayerExit(player)
	local Values = {
		player.leaderstats.Cash.Value,
		player.leaderstats.Rank.Value,
		player.XP.Value
	}
	local playerUserId = "Player_" .. player.UserId
	
	local success, err = pcall(function()
		playerData:SetAsync(playerUserId, Values)
	end)

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

local function onServerClose()
	for _, player in ipairs(Players:GetPlayers()) do
		local Values = {
			player.leaderstats.Cash.Value,
			player.leaderstats.Rank.Value,
			player.XP.Value
		}
		local playerUserId = "Player_" .. player.UserId
		
		local success, err = pcall(function()
			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)

Store XP inside the player instance itself not the ReplicatedStorage folder.

local Player = game:GetService("Players").LocalPlayer
local PlayerRank = script.Parent:WaitForChild("UserFrame"):WaitForChild("PlayerRank")

local leaderstats = Player:WaitForChild("leaderstats")
local Rank = leaderstats:WaitForChild("Rank")

PlayerRank.Text = Rank.Value

Probably just need to add waits to allow for the Gui to load (assuming the references are correct).

It still didn’t work and also my DataStoreScript had 2 errors:

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 XP = Instance.new("IntValue")
	XP.Name = "Exp"
	XP.Parent = player --store exp inside player instead of replicatedstorage

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

	if type(data) == "table" then
		Cash.Value = data[1] or 0
		Rank.Value = data[2] or "Guest"
		XP.Value = data[3] or 0
	else
		print("No data to load!")
	end
end

local function onPlayerExit(player)
	local Values = {
		player.leaderstats.Cash.Value,
		player.leaderstats.Rank.Value,
		player.XP.Value <--- Here
	}
	local playerUserId = "Player_" .. player.UserId
	
	local success, err = pcall(function()
		playerData:SetAsync(playerUserId, Values)
	end)

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

local function onServerClose()
	for _, player in ipairs(Players:GetPlayers()) do
		local Values = {
			player.leaderstats.Cash.Value,
			player.leaderstats.Rank.Value,
			player.XP.Value <--- Here
		}
		local playerUserId = "Player_" .. player.UserId
		
		local success, err = pcall(function()
			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)