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
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.
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)
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)