Owned badge GUI

Hello,

I recently had an idea of making a Gui to see if you own all the badges in my game.
The problem is i just don’t know how i would make a script that checks that and if the user owns the badge the textlabel said “Owned” and if not “You do not own this badge.”

Thanks in advance for the the help!

1 Like

For example,

local BadgeService = game:GetService("BadgeService")
local BadgeId = 123
 
game:GetService("Players").PlayerAdded:Connect(function(player)
    if BadgeService:UserHasBadge(player.UserId, BadgeId) then
        print("The user has this badge")
    else
        print("The user does not have this badge")
    end
end)

In local script try ;

local BadgeService = game:GetService("BadgeService")
local BadgeId = 123
local Player = game.Players.LocalPlayer

If BadgeService:UserHasBadge(Player.UserId, BadgeId) then 
script.Parent.Text = "Owned"
else
script.Parent.Text = "Not Owned"

end

For more information:

https://developer.roblox.com/en-us/api-reference/function/BadgeService/UserHasBadge

1 Like

This works, thanks a lot for your help!

1 Like

UserHasBadge is deprecated, there’s a big warning at the top of its official documentation page indicating so, UserHasBadgeAsync is the newer version & should be used instead. Additionally, the pcall() global should be used to handle any potential errors, i.e; if the BadgeService is down/the client’s Internet connection drops.

local players = game:GetService("Players")
local player = players.LocalPlayer
local badges = game:GetService("BadgeService")
local userHasBadgeAsync = badges.UserHasBadgeAsync

local badgeId = 0 --Change to ID of badge.

local function hasBadge(retries)
	retries = retries or 0
	if retries >= 3 then return end
	
	local success, result = pcall(userHasBadgeAsync, badges, player.UserId, badgeId)
	if success then
		if result then
			print("Player owns the badge!")
		else
			print("Player does not own the badge!")
		end
	else
		warn(result)
		retries += 1
		hasBadge(retries)
	end
end
hasBadge()

https://developer.roblox.com/en-us/api-reference/function/BadgeService/UserHasBadgeAsync

Thanks a lot for your reply but as long if it works I’m happy :smiley: