How to give a player badge when the player joins

Hello Developers, I tried to make a script that when 5 players be in the server in the same server all the 5 players gets a badge can anyone tell me what’s the problem in the script and how to fix it? this what I made but its broken.

local badgeid = 123456
PlayersInTheServer = 0

game.Players.PlayerAdded:Connect(function(player)
	print(player.Name.." Has Joined The Server.")
	PlayersInTheServer = PlayersInTheServer + 1

	if PlayersInTheServer == 5 then
		game:GetService("BadgeService"):AwardBadge(player.UserId, badgeid)
	end
end)
9 Likes

You can just use for loop to loop through online player on the server

if PlayersInTheServer == 5 then
	for i,player in game.Players:GetChildren() do
		game:GetService("BadgeService"):AwardBadge(player.UserId, badgeid)
	end
end
4 Likes

Something like this …

local BadgeId = 123456789

local function giveBadgeToPlayer(player)
    if player:IsA("Player") then
        local badgeService = game:GetService("BadgeService")
        if not badgeService:UserHasBadgeAsync(player.UserId, BadgeId) then
            badgeService:AwardBadge(player.UserId, BadgeId)
          --  print(player.Name .. " has been awarded the badge!")
        else
          --  print(player.Name .. " already has the badge.")
        end
    end
end

local function checkPlayerCountAndAwardBadge()
    local players = game:GetService("Players"):GetPlayers()
    if #players >= 5 then
        for _, player in ipairs(players) do
            giveBadgeToPlayer(player)
        end
    end
end

game:GetService("Players").PlayerAdded:Connect(checkPlayerCountAndAwardBadge)

May need some love, untested.

4 Likes

Thanks for help, I didn’t know that the problem was like this ill try it!

5 Likes

Wow! it’s a little long script.

4 Likes

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