How to Create a "Met the Creator" Badge the Proper Way!

I’m Jonah and I’m going to teach you how to create a script that detects if the creator is in the player’s server and then award them a badge! :slight_smile:

First, we need BadgeService. This will let us award badges to players.

local badgeService = game:GetService("BadgeService")

Second, we will create two variables to store the badge’s ID and the creator’s username.

local badgeService = game:GetService("BadgeService")

local badgeId = 0
local creatorUsername = ""

Third, we need to detect when a player joins your game.

local badgeService = game:GetService("BadgeService")

local badgeId = 0
local creatorUsername = ""

game.Players.PlayerAdded:Connect(function(player)
	
end)

Fourth, we will use an if statement to check if the player’s name is equal to the creator’s name, and if it is, we will use a for loop to award a badge to every player in the server.

local badgeService = game:GetService("BadgeService")

local badgeId = 0
local creatorUsername = ""

game.Players.PlayerAdded:Connect(function(player)
	if player.Name == creatorUsername then
		for _, v in pairs(game.Players:GetPlayers()) do
			badgeService:AwardBadge(v.UserId, badgeId)
		end
	end
end)

If a player joins when the creator is already in the server, no badge will be awarded to them. We can fix this by searching the player list for the creator’s name and if we find a match, we will award the badge to only the player who joined.

local badgeService = game:GetService("BadgeService")

local badgeId = 0
local creatorUsername = ""

game.Players.PlayerAdded:Connect(function(player)
	if player.Name == creatorUsername then
		for _, v in pairs(game.Players:GetPlayers()) do
			badgeService:AwardBadge(v.UserId, badgeId)
		end
	end
	
	if game.Players:FindFirstChild(creatorUsername) then
		badgeService:AwardBadge(player.UserId, badgeId)
	end
end)

Now you have a working “Met the Creator” badge giver!

14 Likes

actually using player userid is much better than the name, due to people can change their name but you cant change your userId

14 Likes

Nice tutorial but there’s one big problem with your code,

you’re checking if the creator joins and then you award a badge to all the players in the server at the moment.

But what if another player joins? They won’t get the badge because PlayerAdded only fires once.

What would make it work (and also take more code) is if you checked if the creator is in the game for every player whenever they join.

But what about all the players who were already in the server? We have to account for them too.

im wrong about the above :arrow_up:

For this you could probably keep your existing code or you can also check every x seconds to see if the creator is in the game (not effective)

Oh and by the way you made a mistake here:

It should instead be

badgeService:AwardBadge(v.UserId, badgeId)

Why? Because you defined “player” in the playerAdded event and that would mean that in this situation, “player” would be the creator so you’d be awarding the creator multiple times

5 Likes

This part of the code will search the players for the creator’s username and award the badge to the player that joined. I may be reading this wrong, but this code does exactly what you mentioned:

if game.Players:FindFirstChild(creatorUsername) then
	badgeService:AwardBadge(player.UserId, badgeId)
end

I fixed this. Thank you!

4 Likes

Keep in mind that badge awarding could error, so you should ideally wrap this in a pcall. Also, a warning is outputted if the user already owns the badge, so you should check to ensure that the player doesn’t own the badge before attempting to award it.

The ideal awarding flow for badges is the following:

All of the following should be wrapped in pcall as any of the listed functions may error.

  1. Use GetBadgeInfoAsync to ensure the badge is enabled. If it is not, cancel the badge awarding operation as it is guaranteed to fail.
  2. Use UserHasBadgeAsync to ensure the player doesn’t already own the badge. If the player already owns the badge, a warning will be outputted while attempting to award, which will appear in the error-log; to mitigate this we should cancel the awarding if the player already owns the badge.
  3. Finally, use AwardBadge to award the badge to the player. This function could still error if Roblox’s badge systems are down, so it should still be wrapped in a pcall.
2 Likes

You should also have a link to Creating and Scripting Badges.

1 Like

Based on some of the replies above I wouldn’t call this the “proper” way. Just “a” way.

2 Likes

How to change creatorUsername to creatorId?

local creatorId = game.CreatorId
1 Like