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 = 2138779893 -- Replace this with your badge ID
Next we need to get the ID of the creator, which is your Roblox user ID. To do this, go to the Roblox website and click on the Profile tab. Then copy the number in the URL.
Now, create a variable for the creator ID and set it equal to your user ID:
local creatorId = 2269948290 -- Replace this with your user ID
First, we want to award players that are already in the server when the creator joins. To detect when the creator joins, we use the PlayerAdded event. We then check if the user ID of the player that joined is equal to creatorId
, and if it matches, we use a for loop to award the badge to all of the players in the game. The code below also checks to make sure the player doesn't already own the badge to prevent warnings in the developer console.
Players.PlayerAdded:Connect(function(player)
-- Award badge to players already in the game if the creator joins
if player.UserId == creatorId then
for i, v in pairs(Players:GetPlayers()) do
if not BadgeService:UserHasBadgeAsync(v.UserId, badgeId) then
BadgeService:AwardBadge(v.UserId, badgeId)
end
end
end
end)
This will award a player the badge if they are already in the server when the creator joins, but what if the creator is in the server and then the player joins? Inside of the PlayerAdded event, we can write code that checks if the creator is in the server, and then award the badge to the player that joined if they don't already have the badge. After writing the code for this, your entire script should look like this:
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 2138779893 -- Replace this with your badge ID
local creatorId = 2269948290 -- Replace this with your user ID
Players.PlayerAdded:Connect(function(player)
-- Awards the badge to players that are already in the server if the creator joins
if player.UserId == creatorId then
for i, v in pairs(Players:GetPlayers()) do
if not BadgeService:UserHasBadgeAsync(v.UserId, badgeId) then
BadgeService:AwardBadge(v.UserId, badgeId)
end
end
end
-- Awards the badge to a player when the join the server if the creator is already in the server
if Players:GetPlayerByUserId(creatorId) ~= nil then
if not BadgeService:UserHasBadgeAsync(player.UserId, badgeId) then
BadgeService:AwardBadge(player.UserId, badgeId)
end
end
end)
If you now go to the Roblox website and play your game, you should receive the badge when you join the game since you are the creator. 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.
You can also test this with an alt account; first join the game on your main account, then join the game using your alt account (usually on another device, such as a phone/tablet). You should receive the badge on your alt account if the creator (your main account) is in the same server.