I need help on how to award badges outside of touching a part

I need help today, on how do I award the player a badge when they hit a certain amount of leaderstats and time played.

Example:
10 coins: Gets the 10 coins badge
100 coins: Gets the 100 coins badge
1000 coins: Gets the 1,000 coins badge

example 2
5 minutes of playtime: Gets the 5 minutes badge
15 minutes of playtime: gets the 15 minutes badge
30 minutes of playtime: Gets the 30 minutes badge
1 hour of playtime: Gets the 1 hour badge.

I found the following scripts from a resource post. I think it might be what you’re looking for.

Leaderstats Badge Script

local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync
local awardBadge = badges.AwardBadge

local badgeId = 0 --Change to ID of badge.
local statName = "" --Change to the stat's name. (For example, if you have a Coins Leaderstats then you would put "Coins"
local badgeValue = 0 --How much stats required to receive the badge. (Example would be when a player gets 10 coins, they will receive the badge).

local function onPlayerAdded(player)
	local function onStatChanged(statValue)
		if statValue >= badgeValue then
			local success, result = pcall(userHasBadge, badges, player.UserId, badgeId)
			if success then
				if not result then
					local success2, result2 = pcall(awardBadge, badges, player.UserId, badgeId)
					if success2 then
						if result2 then
							do end
						end
					else
						warn(result2)
					end
				end
			else
				warn(result)
			end
		end
	end
	
	local leaderstats = player:WaitForChild("leaderstats")
	local stat = leaderstats:WaitForChild(statName)
	stat.Changed:Connect(onStatChanged)
end

players.PlayerAdded:Connect(onPlayerAdded)

Time Played Script:

local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync
local awardBadge = badges.AwardBadge

local badgeId = 0 --Change to ID of badge.
local timeLength = 0 --Change this to a length of time in seconds.

local function giveBadge(player)
	local success2, result2 = pcall(awardBadge, badges, player.UserId, badgeId)
	if success2 then
		if result2 then
			do end
		end
	else
		warn(result2)
	end
end

local function onPlayerAdded(player)
	local success, result = pcall(userHasBadge, badges, player.UserId, badgeId)
	if success then
		if not result then
			task.delay(timeLength, giveBadge, player)
		end
	else
		warn(result)
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
1 Like

Whatever script that is tracking these examples should also check if it fits the criteria to reward these badges. For example, when the play time is updated, if it is equal to or greater than the required time, then the script will reward the badge.