Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make badge counter?

Asked by 8 years ago

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

Log in to vote
2
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

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)
0
I already had an leaderstats, how do i let it work without this leaderstat in the this script? minetrackmania 186 — 8y
1
You remove your if statements and replace it with `cash = countBadges()`. Make sure that the countBadges function is in your script. BlackJPI 2658 — 8y
0
In the leaderboard script ? minetrackmania 186 — 8y
1
See my edit BlackJPI 2658 — 8y
View all comments (8 more)
0
Error on line 17* minetrackmania 186 — 8y
1
line 17 is wrong, it should say: cash.Value = countBadges(player) DevSean 270 — 8y
0
i keep trying diffrent things say still errors minetrackmania 186 — 8y
0
He is right, it should be cash.Value BlackJPI 2658 — 8y
0
Line 16 output: attempt to index local 'stats' (a nil value) minetrackmania 186 — 8y
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() DevSean 270 — 8y
0
I tried now with not my custom leaderboard but with an normal one its still not working minetrackmania 186 — 8y
0
Okay i found the problem, userId hadn't to change! minetrackmania 186 — 8y
Ad

Answer this question