How to give a badge when someone hits a certain stage

Yes, my second time, because last time the script didn’t work so I decided to make a new topic

Code that I have (ServerScriptService):

while wait(1) do
	for i,v in pairs(game.Players:GetChildren()) do
		if v.leaderstats.Stage.Value >= 26 then
			game:GetService("BadgeService"):AwardBadge(v.UserId,2125418931)
		end
	end
end

It works but it doesn’t check if the player already has the badge

What I want is the script to check if the player already has a badge

Thanks.

1 Like

Use BadgeService:UserHasBadgeAsync to check if the player already has the badge before awarding it.

while wait(1) do
	for i,v in pairs(game.Players:GetChildren()) do
		if v.leaderstats.Stage.Value >= 26 then
			if not game:GetService("BadgeService"):UserHasBadgeAsync(v.UserId,2125418931) then
				game:GetService("BadgeService"):AwardBadge(v.UserId,2125418931)
			end
		end
	end
end
2 Likes

Yeah I was about to say that I know I had to use BadgeService:UserHasBadgeAsync for it to check. I just didn’t know how to add it.

local BadgeService = game:GetService("BadgeService")
 
local function awardBadge(player, badgeId)
	-- Fetch badge information
	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(badgeId)
	end)
	
	if success then
		-- Confirm that badge can be awarded
		if badgeInfo.IsEnabled then
			-- Award badge
			local success, result = pcall(function()
				return BadgeService:AwardBadge(player.UserId, badgeId)
			end)
			
			if not success then
				-- the AwardBadge function threw an error
				warn("Error while awarding badge:", result)
			elseif not result then
				-- the AwardBadge function did not award a badge
				warn("Failed to award badge.")
			end
		end
	else
		warn("Error while fetching badge info: " .. badgeInfo)
	end
end

for i,v in pairs(game.Players:GetChildren()) do
		if v.leaderstats.Stage.Value >= 26 then
          if  BadgeService:UserHasBadge(v.UserId, 2125418931) then
      print("already has badge")
    else
            BadgeService:awardBadge(v.UserId, 2125418931)
		end
	end
end

See if this works :smile:

2 Likes

Only works for one script…

1 Like

Wait, I forgot to close the scripts…

You could probably hook up a :GetPropertyChangedSignal(“Value”) instead. I don’t see why we need to call this every second. (This is replacing the true loop)

2 Likes

Try this:

local BadgeService = game:GetService("BadgeService")

local function awardBadge(player, badgeId)
	-- Fetch badge information
	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(badgeId)
	end)

	if success then
		-- Confirm that badge can be awarded
		if badgeInfo.IsEnabled then
			-- Award badge
			local success, result = pcall(function()
				return BadgeService:AwardBadge(player.UserId, badgeId)
			end)

			if not success then
				-- the AwardBadge function threw an error
				warn("Error while awarding badge:", result)
			elseif not result then
				-- the AwardBadge function did not award a badge
				warn("Failed to award badge.")
			end
		end
	else
		warn("Error while fetching badge info: " .. badgeInfo)
	end
end

game:GetService("Players").PlayerAdded:Connect(function(plr)
	if plr.leaderstats.Stage.Value >= 26 then
		if BadgeService:UserHasBadge(plr.UserId, 2125418931) then
			print("already has badge")
		else
			BadgeService:awardBadge(plr.UserId, 2125418931)
		end
	end
end)

Actually scratch that ^

You could make it award the player a badge upon hitting a part when the stage changes?

1 Like
local players = game:GetService("Players")
local badgeService = game:GetService("BadgeService")

players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local stage = leaderstats:WaitForChild("Stage")
	
	stage.Changed:Connect(function(newValue)
		if newValue == 26 then
			badgeService:AwardBadge(player.UserId, 2125418931)
		end
	end)
end)
2 Likes

Bad idea. There is already people who are halfway through the obby.

Instead, I think you can do this

if newValue >= 26 then

1 Like