Awards a badge when it reaches a certain stage (Obby)

Hello, I want to give people badges when it reaches a certain stage. But the problem is that I made the badges way too late and people already have reached a higher stage but won’t get the badges they were supposed to get when they were at a lower stage.

So what I want is a system where it can give people badges when it reaches a certain badge and gives a badge if they already reached a certain stage before.

1 Like

when player joins game run a check, if player stage >= 50 then award stage 50 badge

I don’t really script, so can you show me the script?

		if value >= 91 then

Is that an example?


badgecheck = [
25 = "badgerewardid",
]
game.Players.PlayerAdded:Connect(function(P)
   for i,v in pairs(badgecheck) do 
if P.Stage.Value >= i then 
badgeservice award(v)
end

end)

^ psuedocode

1 Like

Do I put that in ServerScriptService?

https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded
https://developer.roblox.com/en-us/api-reference/class/BadgeService

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

My friend made this for me, put a normal script in ServerScriptService and change 6 to any stage you want to award the badge and change 2125002441 to the badge ID you want to use.

1 Like

That works but is inefficient and could also error if for example the BadgeService is unavailable.

local players = game:GetService("Players")
local badges = game:GetService("BadgeService")

local badgeId = 0 --Change to ID of badge.
local stageForBadge = 0 --Change to stage required for badge.

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

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 0 --Starting stage.

	stage.Changed:Connect(function(stageNumber)
		if stageNumber >= stageForBadge then
			local success, result = pcall(function()
				return badges:UserHasBadgeAsync(player.UserId, badgeId)
			end)

			if success then
				if not result then --Doesn't have badge.
					local success2, result2 = pcall(function()
						return badges:AwardBadge(player.UserId, badgeId)
					end)

					if success2 then
						if result2 then
							print("Badge successfully awarded to "..player.Name..".")
						end
					else
						warn(result2)
					end
				end
			else
				warn(result)
			end
		end
	end)
end)
3 Likes

I’m kinda late to respond to this but, just want to mention that I already have a leaderstats. So what would it look like without the leaderstats.

1 Like