How to give each player a badge

Hey there devs, I am not a very good scripter and I am trying to give the player a badge for completing one ending on a game. This is in a server script, and I’m not sure how I would then get the player’s UserId and use that to give them the badge. The UserId should be going in the blank spot, but I’m not sure how to get it. If you know how, please let me know!

local badgeservice = game:GetService("BadgeService")
local badgeId = 2124728298
local Players = game:GetService("Players")

		for i, player in pairs(Players:GetPlayers()) do
			badgeservice:AwardBadge(  , badgeId)
		end

You can just get their UserId from the player variable. Basically the for loop will loop through every player, and make a variable called “I” which is the index or how many loops it has already done, and a variable called player with is the current player it is looping through.

local badgeservice = game:GetService("BadgeService")
local badgeId = 2124728298
local Players = game:GetService("Players")

for i, player in pairs(Players:GetPlayers()) do
	badgeservice:AwardBadge( player.UserId , badgeId)
end
2 Likes

Ok so I was typing it out, @XdJackyboiiXd21 beat me to it. That is exactly how you would do it. If you want to assign it to a function, you would want to do:

local badgeservice = game:GetService("BadgeService")
local badgeId = 2124728298
local Players = game:GetService("Players")

local function giveBadges() -- allows you to call giveBadges to give every player in the server a badge.
	for i, player in pairs(Players:GetPlayers()) do
		badgeservice:AwardBadge(player.UserId , badgeId)
	end
end

giveBadges()

Additionally, you could use parameters to give different badges:

local badgeservice = game:GetService("BadgeService")
local badgeId = 2124728298
local Players = game:GetService("Players")

local function giveBadges(id) -- allows you to call giveBadges (with the ID as the first parameter) to give every player in the server a badge.
	for i, player in pairs(Players:GetPlayers()) do
		badgeservice:AwardBadge(player.UserId , id)
	end
end

giveBadges(badgeId)

Hope this helped!

6 Likes