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