Global leaderboard is breaking datastore

So I recently made a global leaderboard GUI. For some reason after I have implemented it into my game my data store has now stopped working. I’ll send both scripts. I should mention that the Leaderboard holds the value of the wins still until the player joins the game, then it resets to 0 to match the leaderstats.

DataStore script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")

local Players = game:GetService("Players")

game:BindToClose(function()
	wait(2)
end)

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats

	local data
	local success, errormessage = pcall(function()
		data = DataStore1:GetAsync(player.UserId.."-wins")
	end)
	if success then
		wins.Value = data
	else
		print("There was an erro whilst getting your data")
		warn(errormessage)
	end
end)



Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DataStore1:SetAsync(player.UserId,player.leaderstats.Wins.Value)
	end)
	if success then
		print("Player Data Successfully Saved")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

Leaderboard Gui Script:

local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard2 = DataStoreService:GetOrderedDataStore("WinsLeaderboard2")

local function updateLeaderboard()
	local success, errormessage = pcall(function()
		local Data = WinsLeaderboard2:GetSortedAsync(false, 5)
		local WinsPage = Data:GetCurrentPage()
		for Rank, data in ipairs(WinsPage)do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local Wins = data.value
			local isOnLeaderboard = false
			for i, v in pairs(game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren())do
				if v.Player.Text == Name then
					isOnLeaderboard = true
				end
			end
			
			if Wins and isOnLeaderboard == false then
				local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
				newLbFrame.Player.Text = Name
				newLbFrame.Wins.Text = Wins
				newLbFrame.Rank.Text = "#"..Rank
				newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.08 * #game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()), 0)
				newLbFrame.Parent = game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder
			end
		end
	end)
	if not success then 
		print(errormessage)
	end
end


while true do
	for _, player in pairs(game.Players:GetPlayers())do
		WinsLeaderboard2:SetAsync(player.UserId, player.leaderstats.Wins.Value)
	end
	for _, frame in pairs(game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren())do
		frame:Destroy()
	end
	updateLeaderboard()
	print("Updated!")
	wait(60)
end

I’m not sure why the wins reset now, it was working perfectly before I added in the GUI, so I assume it has something to do with that.

1 Like

Is there any error in the output?

No, there is not an error in the output. Wins act normal until I rejoin the game and then they disappear, it’s like some value in the Global leaderboard is conflicting with the wins value.

I am noticing that you are using two different DataStores:
DataStore1 & WinsLeaderboard2.
You only seem to be getting data from the DataStore1 one, and not the WinsLeaderboard2.

Please doublecheck your spelling and add prints to detect where your code might stop if above is not your issue.
You should also see if it gives you any errors.

I don’t seem to have any errors. Maybe my issue is that it’s setting Async twice? Not sure. I am not very familiar with data stores or global leaderboards so I tried doing :GetAsync in the while loop instead of :SetAsync to see what that did but the result was the same.

Updated DataStore:

local DataStoreService = game:GetService("DataStoreService")
local DataStore2 = DataStoreService:GetDataStore("DataStore2")

local Players = game:GetService("Players")

game:BindToClose(function()
	wait(2)
end)

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats

	local data
	local success, errormessage = pcall(function()
		data = DataStore2:GetAsync(player.UserId.."-wins")
	end)
	if success then
		wins.Value = data
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
end)



Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DataStore2:SetAsync(player.UserId,player.leaderstats.Wins.Value)
	end)
	if success then
		print("Player Data Successfully Saved")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

Updated GlobalLeaderboard:

local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard3 = DataStoreService:GetOrderedDataStore("DataStore2")

local function updateLeaderboard()
	local success, errormessage = pcall(function()
		local Data = WinsLeaderboard3:GetSortedAsync(false, 5)
		local WinsPage = Data:GetCurrentPage()
		for Rank, data in ipairs(WinsPage)do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local Wins = data.value
			local isOnLeaderboard = false
			for i, v in pairs(game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren())do
				if v.Player.Text == Name then
					isOnLeaderboard = true
				end
			end
			
			if Wins and isOnLeaderboard == false then
				local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
				newLbFrame.Player.Text = Name
				newLbFrame.Wins.Text = Wins
				newLbFrame.Rank.Text = "#"..Rank
				newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.08 * #game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()), 0)
				newLbFrame.Parent = game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder
			end
		end
	end)
	if not success then 
		print(errormessage)
	end
end


while true do
	for _, player in pairs(game.Players:GetPlayers())do
		WinsLeaderboard3:SetAsync(player.UserId, player.leaderstats.Wins.Value)
	end
	for _, frame in pairs(game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren())do
		frame:Destroy()
	end
	updateLeaderboard()
	print("Updated!")
	wait(60)
end

I tried to disable the global leaderboard script and my datastore just doesn’t work anymore. It was working fine before I added in the global leaderboard a couple days ago. It says in the output that the Player Data was Successfully Saved.

So I solved this, I just had to make a Data store script separate from my leaderstats. Here’s the script if anybody is interested.

Datastore:

local datastore = game:GetService("DataStoreService")

local plrdata = datastore:GetDataStore("PlayerData")

local player = game:GetService("Players")



player.PlayerAdded:Connect(function(plr)

	local id = 'Player_'..plr.UserId

	local data = plrdata:GetAsync(id)

	if data then

		plr.leaderstats.Wins.Value = data

	else

		plr.leaderstats.Wins.Value = 0

	end

end)


player.PlayerRemoving:Connect(function(plr)

	local success, err = pcall(function()

		local id = 'Player_'..plr.UserId

		plrdata:SetAsync(id,plr.leaderstats.Wins.Value)

	end)

	if not success then

		warn('Could not save')

	end

end)