How To Prevent Test Players Showing on Leaderboard?

You know how when you test in studio with multiple players by yourself it creates “Player (Amount)”?

How can I prevent these people on the leaderboard? It errors my leaderboard because they aren’t real players.

local statsName = "Kills"
local maxItems = 50
local minValueDisplay = 1
local abbreviateValue = true
local updateEvery = 60

local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local dataStore = dataStoreService:GetOrderedDataStore("GlobalLeaderboard_"..statsName)

--remove player data if needed
--dataStore:RemoveAsync(userID, nil)

local contents = script.Parent:WaitForChild("List")
local template = script:WaitForChild("List")

local colors = {
	Default = Color3.fromRGB(255, 255, 255),
	Gold = Color3.fromRGB(255, 215, 0),
	Silver = Color3.fromRGB(192, 192, 192),
	Bronze = Color3.fromRGB(205, 127, 50)
}

function comma_value(amount)
	local formatted = tostring(amount)
	while true do  
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
		if k == 0 then break end
	end
	return formatted
end

local function getItems()
	local data
	local success, errorMessage = pcall(function()
		data = dataStore:GetSortedAsync(false, maxItems, minValueDisplay)
	end)

	if success then
		for position, v in ipairs(data:GetCurrentPage()) do
			local userId = v.key
			local value = v.value
			local username = "[N/A]"
			local color = colors.Default

			local success, result = pcall(function()
				return players:GetNameFromUserIdAsync(userId)
			end)

			if success then
				username = result
			end

			if position == 1 then
				color = colors.Gold
			elseif position == 2 then
				color = colors.Silver
			elseif position == 3 then
				color = colors.Bronze
			end

			local item = template:Clone()
			item.Name = username
			item.PlayerIcon.Image = players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
			item.LayoutOrder = position
			item.Number.TextColor3 = color
			item.Number.Text = "#" .. position
			item.Username.Text = "@" .. username
			item.Stats.Text = "$" .. comma_value(value)
			item.Parent = contents
		end
	else
		warn(errorMessage)
	end
end

local function updatePlayerData()
	for _, player in pairs(players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local statsValue = leaderstats:FindFirstChild(statsName)
			if statsValue then
				pcall(function()
					dataStore:UpdateAsync(player.UserId, function()
						return tonumber(statsValue.Value)
					end)
				end)
			end
		end
	end
end

local function clearContents()
	for _, item in pairs(contents:GetChildren()) do
		if item:IsA("Frame") then
			item:Destroy()
		end
	end
end

updatePlayerData()
clearContents()
getItems()

while task.wait(updateEvery) do
	updatePlayerData()
	clearContents()
	getItems()
end
if player.UserId < 0 then print("Dont save!") return end

I personally don’t have these issues b/c I am very pcall heavy when it comes to datastores, and also b/c I scope out and use local functions every time I am getting or setting data using datastores (you should probably do that even just for organization.

However the easiest way, is that every time you want to get or set data, make sure the userId is greater than 0.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.