Can a Script finds a badge in the Players inventory?

Hello Develepers, i am working on a Script, that show’s the Player an imagebutton ( which IS in the StarterGUI in a frame ) if the player has a certain badge, i already know IT works with a Gamepass, but does that also Work with a badge ?

( If this IS in the wrong category please Tell me )

5 Likes

Do you mean something like this?

4 Likes

if a script can access a badge and perform a certain function if the player has the badge, then that’s what I meant (sorry, I don’t speak English very well, which is why I could only roughly translate the message in the picture, I write most Things in the Develeperforum with the help of a translator):disappointed_relieved: (maybe you can send me the link to the website, were you have found the informations )

3 Likes
local BadgeService = game:GetService("BadgeService")

local badgeId = 0 --replace with your badge id
local userId = 0 --replace with player user id

--you may want to pcall this if you want to ensure it wont error/break the code
if BadgeService:UserHasBadgeAsync(userId, badgeId) then
	print("Badge "..badgeId.." owned by "..userId.."!")
end
6 Likes

here is the link to the roblox documentation:
BadgeService | Documentation - Roblox Creator Hub

3 Likes

Thank you for the scripts and the answers but i mean something like this:

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

local badgeID = 123456789 -- I will change the ID soon

local function checkBadge(player)
    local success, hasBadge = pcall(function()
        return BadgeService:UserHasBadgeAsync(player.UserId, badgeID)
    end)
    
    if success and hasBadge then
        local playerGui = player:WaitForChild("PlayerGui")
        local screenGui = playerGui:FindFirstChild("OpenFrame")
        if screenGui then
            local haveBadgeFrame = screenGui:FindFirstChild("havebadge")
            if haveBadgeFrame then
                haveBadgeFrame.Visible = true
            end
        end
    else
        warn("Failed to check badge or player does not have the badge")
    end
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        checkBadge(player)
    end)
end)

If something is one please tell me what won’t work ( i have not try it jet)

2 Likes
Old Reply

This is making the frame visible, not the image button.
At least, from what I’m understanding, the image button is in the frame, and you want to make the image button visible.


I’ve read the script, and it turns out that that is probably intentional.

I have also tested it, and it works.

3 Likes

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