To learn how to create a badge on Roblox, read this tutorial. Once you have created your badge, load up your game and insert a Script into ServerScriptService. To award a player a badge, we will need to use BadgeService as well as the Players service.
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
We also need to get our badge ID to award it using code. To do this, click on your badge on the Roblox website and copy the second number in the URL (if there is only one number, then copy that).
Then we can create a variable for the badge ID and set it equal to the badge ID number:
local badgeId = 2131907984 -- Replace this with your badge ID
Next, we need to write code that will award players the badge when they enter the game. To detect when a player joins the game, we use the PlayerAdded event. We then want to wait one second before awarding the badge so that the player can see that they were awarded the badge.
We then check to see if the player already owns the badge using the UserHasBadgeAsync function, and if they don't own the badge, we award the badge using the AwardBadge function. Both of these functions take in two parameters: the player's user ID and the badge ID. If the player already owns the badge and we try to award it again, it can create warnings in the developer console so it is better to check that they don't already have the badge.
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 2131907984 -- Replace this with your badge ID
Players.PlayerAdded:Connect(function(player)
task.wait(1)
local playerHasBadge = BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
if not playerHasBadge then
BadgeService:AwardBadge(player.UserId, badgeId)
end
end)
If you now go to the Roblox website and play your game, you should receive the badge when you join the game. In some cases, you will miss the badge award notification if you take a long time to load into the game. Check the badges section on your Roblox profile to confirm you were awarded the badge.