Solved How to make gui transparency go to 0 when you own the badge 0.5 when you dont

How can i do this? i am new to coding and so dont know much i am still learning i have a hard time with badges in code i want it so when you open the achivements menu it shows as 0.5 transparency if you dont own the badge 0 transparency if you do i will include screenshots below i am also hoping for a script that i can edit easy if you cant do that then its fine
image


image

1 Like

You can use the UserHasBadgeAsync() function provided by the BadgeService, and this will return back a bool (True/False) value if they have the badge or not

This is a yielding function as well, so calling it will pause the Script & prevent its lines of codes from advancing any further until a result has been provided from the function

But since you have more than 1 badge to check, we’ll have to use a table which will hold all of our current Badges that we want to check

-- Put a LocalScript inside your Gui here 
local Plr = game.Players.LocalPlayer
local BS = game:GetService("BadgeService")
local Gui = script.Parent
local Badges = {
   BadgeName1 = 0000000000, -- Placeholder ID, the comma in this (,) separates each thing inside the table
   BadgeName2 = 0000000001,
}

local function CheckBadge(BadgeID)
   local BadgeOwned = BS:UserHasBadgeAsync(Plr.UserId, BadgeID)
   return BadgeOwned
end

for BadgeName, BadgeID in pairs(Badges) do
   local Check = CheckBadge(BadgeID)
   local FrameCheck = Gui:FindFirstDescendant(BadgeName)

   print("Badge Owned:", Check)
   print("Frame in Gui:", FrameCheck)

   if FrameCheck then -- Checking if the Frame Name exists in your Gui
       if Check == true then -- Checking if the Badge the player owns is true, if so we can change the Frame Transparency!
           FrameCheck.BackgroundTransparency = 0 
           FrameCheck.ImageTransparency = 0 -- Not sure if you want the image transparent as well
       else
           FrameCheck.BackgroundTransparency = 0.5
           FrameCheck.ImageTransparency = 0.5 -- Not sure if you want the image transparent as well
       end
   end
end

And don’t hesitate to add prints as well, cause that’s essential for script debugging & figuring out what’s wrong inside your code

2 Likes

I will try this what do i do if i wanna add more badges?

Then you can just add more to the “Badges” table like so:

local Badges = {
   BadgeName1 = 0000000000, -- Placeholder ID, the comma in this (,) separates each thing inside the table
   BadgeName2 = 0000000001,
   BadgeName3 = 0000000002,
   BadgeName4 = 0000000003,
}

Thank you i will try this in a test game

do i put the script in screengui or my gui (the frame)

Script isnt working correctly ?

hello? you there? the script dosent work

Parent the LocalScript in your ScreenGui, yes

Make sure to add prints to know what does & doesn’t work, it’s useful for debugging purposes

i found the problem but i need you to help me fix it i found it dosent run one part of the script at all


2 of the prints it dosent run the last 2

1 Like

i own one of the badges yet it dosent work
image

Print what BadgeOwned gives you inside the CheckBadge function, also make sure to check out what Check & FrameCheck prints back as a result so that there is a proper frame name there

what do you mean? i’m confused

i think something is just wrong with the script in some way?

You can do something like this:

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

local player = PS.LocalPlayer

local function SetBadgeTransparency(imageLabel: ImageLabel, badgeId: number)
	imageLabel.ImageTransparency = if BS:UserHasBadgeAsync(player.UserId, badgeId) then 0 else .5
end

Just add prints to any local variables that you see, the loop should work fine regarding all the current Badges

(Lemme re-edit my code a bit so that you can try it again)

No prints showed in output what does that mean?

Something is wrong with your code. Take a look back and follow your steps.

you mean his code? i didnt edit anything except badge id’s so i dont understand much of it really

Where did you place the script? Is it under your GUI?