How to award badge to someone verified in my experience?

Hey devs,

So I recently added few badges into my experience including welcome, met the owner and secret badges. And i would like to create more unique badge, by making it hard to obtain.

So i got an idea that i will make something like “Verified user joined” or something related to it. Basically the problem is that I don’t know how to script it. I tried editing the welcome badge and adding few lines of codes but didn’t worked. After that i tried looking for some YouTube tutorials, but I’m not able to find any matching my inquiry.

So now I’m here at forum begging you guys for help, because i have no clue what else to do.

Basically i want the badge to work same way as the welcome badge, when you join, but this badge will be awarded to only verified users. (Quickly said: Welcome badge for verified users.)

I would really appreciate any reply or tip!

Have a great day,
-jeziskrista

2 Likes

I wrote a quick function:

function CheckIfPlayerIsVerified(UserId : number) : boolean
	if type(UserId) ~= "number" then return end
	
	local Player = game.Players:GetPlayerByUserId(UserId)
	if not Player then warn("Player not found") return end
	
	return Player.HasVerifiedBadge
end

Heres how you can implement it, this was with a little help from the documentation:

local BadgeService = game:GetService("BadgeService")
local BADGE_ID = 0 -- Your badge ID here

function CheckIfPlayerIsVerified(UserId : number) : boolean
	if type(UserId) ~= "number" then return end

	local Player = game.Players:GetPlayerByUserId(UserId)
	if not Player then warn("Player not found") return end

	return Player.HasVerifiedBadge :: boolean
end

game.Players.PlayerAdded:Connect(function(Player : Player)
	local IsVerified = CheckIfPlayerIsVerified(Player.UserId)
	if not IsVerified then return end
	if BADGE_ID <= 0 then return end
	
	local success, result = pcall(function()
		return BadgeService:GetBadgeInfoAsync(BADGE_ID)
	end)
	
	if not success then warn("Something went wrong: ".. result) return end
	if not result.IsEnabled then return end
		
	local awardSuccess, AwardResult = pcall(function()
		return BadgeService:AwardBadge(Player.UserId, BADGE_ID)
	end)
			
	if not awardSuccess then warn("Error while giving badge: "..result) return end
	if not AwardResult then warn("Failed to give badge.") return end			
end)
4 Likes
local BadgeService=game:GetService("BadgeService")

function AwardBadge(player,badge)
	local success,hasBadge=pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId,badge)
	end) if not success then warn("CheckingError")return end

	if not hasBadge then
		local success,badgeInfo=pcall(function()
			return BadgeService:GetBadgeInfoAsync(badge)
		end)
		
		if success and badgeInfo.IsEnabled then
			local awarded,errorMessage=pcall(function()
				BadgeService:AwardBadge(player.UserId,badge)
			end) if not awarded then warn("AwardError: ",errorMessage)
			else --print("Awarding Badge")
			end
		else warn("FetchError!")
		end
	end
end

--AwardBadge(player, 123456789)

1 Like

Thank you so much!! Appreciate it!

2 Likes

Hey! Thank you so much for your effort. I would love to put the solution also to you, but sadly i can pick only one post as a solution, and the guy above you was first.

Thanks again!

2 Likes

Just a question, can i copy and paste the script without anything needed to be changed? (I already change the badge id)

1 Like

I think you can, I can’t test it because I don’t have a badge to test it with, but try it out, return back to the forum if you get an error or need help.

2 Likes

Sure thing, I have verified friend, so he can test it out for us.

2 Likes

I’m used to it … glad you found what you needed!

2 Likes

My friend will sadly test in Monday, he’s on vacation now.

2 Likes

So sadly, the script didn’t worked.

I quoted part of script that may cause the issue, but im mot fully sure.

I will be again thankful for any help!

Interesting, I modified it a little, to add more concrete debugging features, maybe this will help with indentifying the problem:

local BadgeService = game:GetService("BadgeService")
local BADGE_ID = 0 -- Paste ID here

function CheckIfPlayerIsVerified(UserId : number) : boolean
	local Player = game.Players:GetPlayerByUserId(UserId)
	if not Player then warn("Player not found")	return false end

	return Player.HasVerifiedBadge
end

function AwardBadgeToPlayer(UserId : number) : ()
	if BADGE_ID <= 0 then warn("Invalid badge ID.") return end
	if not CheckIfPlayerIsVerified(UserId) then warn("Player is not verified.") return end

	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(BADGE_ID)
	end)

	if not success then	warn("Failed to get badge info: " .. tostring(badgeInfo)) return end
	if not badgeInfo.IsEnabled then warn("Badge is not enabled.") return end

	local hasBadgeSuccess, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(UserId, BADGE_ID)
	end)

	if not hasBadgeSuccess then warn("Failed to check if player has badge: " .. tostring(hasBadge)) return end

	if hasBadge then warn("Player already has the badge.") return end

	local awardSuccess, awardResult = pcall(function()
		return BadgeService:AwardBadge(UserId, BADGE_ID)
	end)

	if not awardSuccess then warn("Failed to award badge: " .. tostring(awardResult)) return end
	if not awardResult then	warn("Badge award failed.") return end

	print("Badge awarded successfully.")
end

game.Players.PlayerAdded:Connect(function(Player : Player)
	AwardBadgeToPlayer(Player.UserId :: number)
end)

Hey! I will for sure try that for sure. I also tried making the script with Roblox AI assistant.

Basically the AI generated this script: `local BadgeService = game:GetService(“BadgeService”)
local Players = game:GetService(“Players”)

local BADGE_ID = 0 – Replace with your custom badge ID

local function onPlayerAdded(player)
– Check if the player is verified
local success, isVerified = pcall(function()
return player.Verified
end)

if success and isVerified then
    -- Award the badge
    local awardSuccess, result = pcall(function()
        return BadgeService:AwardBadge(player.UserId, BADGE_ID)
    end)

    if not awardSuccess then
        -- the AwardBadge function threw an error
        warn("Error while awarding badge:", result)
    elseif not result then
        -- the AwardBadge function did not award a badge
        warn("Failed to award badge.")
    end
end

end

Players.PlayerAdded:Connect(onPlayerAdded) `

What are your thoughts? Will it work?

I will follow up with an update.

The AI one probably wont work because of this:

player.Verified is not a property of the player.

1 Like

While this should work, you are doing a lot of unnecessary work, and needlessly complicating the program.

There is no reason for you to create an entire function to check if a player is verified by their userId, when your actual use-case already provides you with their player instance.

There is no reason to check if the badge id is less than or equal to 0, as the badge id would be hard coded, and never change. Even if it for whatever reason did change (it really shouldn’t), it would be better to let the script error instead of failing silently.

There is no reason to fetch the badge info, just to check if it is enabled, especially not when you just have the script silently fail. If you absolutely want this check here, at least add a proper error message.

And instead of using warn and then immediately returning out of the function. Just use error, so it is properly logged on the website.

This mess of a script could be boiled down to:

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 0 -- Your badge ID here

Players.PlayerAdded:Connect(function(Client: Player)
    if not Client.HasVerifiedBadge then return end

    local succ, res = pcall(function()
        return BadgeService:AwardBadge(Client.UserId, BADGE_ID)
    end)

    if not succ then error(res) end
    if not res then error("Something went wrong while awarding badge.") end
end)

If you really want to do proper checks, it would be wiser to check if the player already has the badge.

1 Like

True, thanks for noticing. I will probably try reworking the script.

1 Like

Thanks, I’ll check this out as soon as possible. I’m currently away from my home.

Hey Stef,

Thank you so much! I tried inserting your script and it worked.

Thanks a lot again, jeziskrista

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.