How would I award a badge if a player got X amount of points in leaderboard?

I am planning on awarding badges to a player if they reach 1, 10, 100, and 1000 survival(s). How would that be done in this code?

local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local survivals = Instance.new("IntValue")
	survivals.Name = "Survivals"
	survivals.Value = 0
	survivals.Parent = leaderstats
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = leaderstats

	-- Load player's data from the data store
	local userId = player.UserId

	-- Attempt to retrieve the player's survival data from the data store
	local success, data = pcall(function()
	return playerDataStore:GetAsync(userId)
	end)

	if success then
		if type(data) == "table" and data.survivalStats and data.coinStats then
			survivals.Value = data.survivalStats
			coins.Value = data.coinStats
		else
			survivals.Value = 0
			coins.Value = 0
		end
	else
		warn("Failed to retrieve survival data for player " .. player.Name)
	end
	end
game.Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerDied)
Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:FindFirstChild("Humanoid").Died:Connect(function()
			onPlayerDied(plr)
		end)
	end)
end)
local function playerRemoving(plr)
	local success, data = pcall(function()
		local plrstats = {
			survivalStats = plr.leaderstats.Survivals.Value,
			coinStats = plr.leaderstats.Coins.Value
		}
		return playerDataStore:SetAsync(plr.UserId, plrstats)
	end)
	if not success then warn("Error while saving data: "..data) end
end
function startNewRound()
	-- Clear the table at the start of a new round
	playerTable = {}
	-- Re-add all players to the table
	print("Starting")
	for _, player in ipairs(Players:GetPlayers()) do
		playerTable[player.Name] = {
			["isAlive"] = true
		}
	end
end


function endRound()
	print("Ended")
	local survivors = {} -- Table to store the remaining survivors
	for _, player in ipairs(Players:GetPlayers()) do
		if playerTable[player.Name] and playerTable[player.Name]["isAlive"] then
			table.insert(survivors, player) -- Add the player to the survivors table
			local leaderstats = player:FindFirstChild("leaderstats")
			if leaderstats then
				local survivals = leaderstats:FindFirstChild("Survivals")
				if survivals then
					survivals.Value = survivals.Value + 1
					print("Added a survival")
				end
				local coins = leaderstats:FindFirstChild("Coins")
				if coins then
					coins.Value += script.CoinsAmount.Value
				end
			end
		end
	end

At the point where the “endRound” function increases the player’s “survivals” stat, check if they have reached a threshold for one of the badges, and if they have, use BadgeService:AwardBadge() to award the badge to them. Here’s an example:

local survivalBadges = {
    [1] = 0, -- Replace "0" with the Badge ID for 1 win
    [10] = 0, -- Same as above, but the Badge ID for 10 wins
    [100] = 0, -- Same idea, but for 100 wins
    [1000] = 0 -- Same idea, but for 1000 wins
}

local function badgeCheck(player, currentSurvivals)
    for survivals, badgeId in survivalBadges do
        local success, alreadyHasBadge = pcall(function()
            return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
        end)

        if not success then
            warn("BadgeService:UserHasBadgeAsync() call failed")

        elseif
            success -- If the function within the pcall succeeded / did not throw an error
            and currentSurvivals >= survivals -- AND if the player's current survivals is greater than or equal to the requirement for one of the badges
            and not alreadyHasBadge -- AND the player does not already have the badge...
        then
            BadgeService:AwardBadge(player.UserId, badgeId) -- Award the player with the given badge
        end

    end
end

function endRound()
	print("Ended")
	local survivors = {} -- Table to store the remaining survivors
	for _, player in ipairs(Players:GetPlayers()) do
		if playerTable[player.Name] and playerTable[player.Name]["isAlive"] then
			table.insert(survivors, player) -- Add the player to the survivors table
			local leaderstats = player:FindFirstChild("leaderstats")
			if leaderstats then
				local survivals = leaderstats:FindFirstChild("Survivals")
				if survivals then
					survivals.Value = survivals.Value + 1
					print("Added a survival")
                    badgeCheck(player, survivals.Value) -- Newly added code
				end
				local coins = leaderstats:FindFirstChild("Coins")
				if coins then
					coins.Value += script.CoinsAmount.Value
				end
			end
		end
	end
end
3 Likes

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