Badge award issue

Hello.

I want players to get badges for reaching certain percentages in my game (percentages are the same as stages in an obby game, it’s just a different name). I don’t want badges to be awarded when players hit checkpoints, but by using the “Percent” leaderstat.

The problem is that when I test the game, even with the Roblox client, it just doesn’t work. When I reach 5%, which should award the first badge, nothing happens. Not even an error in the development console.

I found something similar on the DevForum, but the solution was to run the game in a public server, which didn’t work for me. I even asked ChatGPT to analyze my script and it didn’t find the cause of the problem, but I don’t know if I should trust it.

The script is a server-side script, parented to ServerScriptService. I also checked that I didn’t already have the badges in my inventory, and I don’t.

local badgeService = game:GetService("BadgeService")

local badgeMapping = {
	[5] = 2148358540,
	[10] = 2148361017,
	[15] = 2148361048,
	[20] = 2148361074,
	[25] = 2148361131,
	[30] = 2148424927,
	[35] = 2148424956,
	[40] = 2148424992,
	[45] = 2148425014,
	[50] = 2148425126,
	[55] = 2148474890,
	[60] = 2148474912,
	[65] = 2148474948,
	[70] = 2148474990,
	[75] = 2148475047,
	[80] = 2148503733,
	[85] = 2148503748,
	[90] = 2148503767,
	[95] = 2148503797,
	[100] = 2148503914
}

game:GetService("Players").PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local percent = leaderstats:WaitForChild("Percent")

	for targetPercent, badgeId in pairs(badgeMapping) do
		if percent.Value >= targetPercent then
			badgeService:AwardBadge(player.UserId, badgeId)
		end
	end
end)

Excuse me if it’s a stupid mistake, I’m still learning to script.

Could you please send a LocalFile, please to exactly see where’s the issue so more programmers can fix it without struggling?

1 Like

Your system only checks when the player enters, try making the verification occur every time the percentage changes using:

game:GetService("Players").PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local percent = leaderstats:WaitForChild("Percent")

	percent.Changed:Connect(function(value)
		for targetPercent, badgeId in pairs(badgeMapping) do
			if percent.Value >= targetPercent then
				badgeService:AwardBadge(player.UserId, badgeId)
			end
		end
	end)
end)
1 Like

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