Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make badge counter?

Badge script counter I tried to make a script that counts players badges, why does it not work? No error or output. Script is an normal one in workspace

wait(2)
game.Players.PlayerAdded:connect(function(plr)
	local stats = plr:findFirstChild("leaderstats")
	local cash = stats:findFirstChild("Badge")

	if game:GetService("BadgeService"):UserHasBadge(plr.userId,229929415) then
		cash = cash.Value +1
	elseif game:GetService("BadgeService"):UserHasBadge(plr.userId,229929797) then
		cash = cash.Value +1
	elseif game:GetService("BadgeService"):UserHasBadge(plr.userId,229929912) then
		cash = cash.Value +1
	end
	
end)

thanks

1 answer

✓ Accepted answer

There isn't enough code here to determine exactly what you problem is. For example, I don't know if you are instantiating the leaderstats or not.

However there is a glaring logic error that I can see. You are using elseif, which will only execute if the condition of the if statement that came before it wasn't true. You want to count EVERY badge the player owns not just the first one.

Cascading if statements is not the best way to accomplish this. It would be more efficient to loop over an array of the badge IDs.

####Example:

local BadgeService = game:GetService("BadgeService")
local badges  = {229929415, 229929797, 229929912}

local function countBadges(player)
	local numBadges = 0
	for _, badge in next, badges do
		if BadgeService:UserHasBadge(player.userId, badge) then
			numBadges = numBadges + 1
		end
	end
	return numBadges
end

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local badges = Instance.new("IntValue", leaderstats)
	badges.Value = countBadges(player)
end)

EDIT

local BadgeService = game:GetService("BadgeService")
local badges  = {229929415, 229929797, 229929912}

local function countBadges(player)
	local numBadges = 0
	for _, badge in next, badges do
		if BadgeService:UserHasBadge(player.userId, badge) then
			numBadges = numBadges + 1
		end
	end
	return numBadges
end

game.Players.PlayerAdded:connect(function(player)
	local stats = player:findFirstChild("leaderstats")
	local cash = stats:findFirstChild("Badge")
	cash.Value = countBadges()
end)

12 comments

0

I already had an leaderstats, how do i let it work without this leaderstat in the this script?

1

You remove your if statements and replace it with cash = countBadges(). Make sure that the countBadges function is in your script.

0

In the leaderboard script ?

1

See my edit

0

Error on line 17*

1

line 17 is wrong, it should say: cash.Value = countBadges(player)

0

i keep trying diffrent things say still errors

0

He is right, it should be cash.Value

0

Line 16 output: attempt to index local 'stats' (a nil value)

1

You haven't defined the leaderstats, if you use the original answer it defines the leaderstats for you. @BlackJPI, line 17 is still wrong on your edit. It should say countBadges(player). Also, "player.userId" is deprecated and you should use "player.UserId"If you have defined the leaderstats, change the code so it uses :WaitForChild() instead of :findFirstChild()

0

I tried now with not my custom leaderboard but with an normal one its still not working

0

Okay i found the problem, userId hadn't to change!

This discussion is preserved as it was. New answers, comments, and votes are closed.