How to make a UI appear if a player has earned a specific badge?

I’m trying to make a level system by awarding a badge for every level completed. By earning the badge, I want the player to be able to see an otherwise invisible button on the main menu to go to the next level.
I used many websites, such as this, and this one, but it didn’t really have a solution.
Note: No errors appear in the output.
I tried using the print() on different lines to see how far the code would go without breaking, but none would be printed, even if it was on the first line (after the definitions).

Edit: The first hyperlink has code to go off of but it has errors or something I’m not too sure.

5 Likes

Well if you are already awarding that badge, in the code that you reward the badge (and right after you reward it) get the player gui local playerGui = Player.PlayerGui then get the screengui and (if you need) frame or something local screengui = playerGui.ScreenGui and set screengui.Enabled = true (if you are using frame use .Visible instead)

2 Likes

How would it work if every level can be selected from the same screen, and you have to get the badges in order to get to the next one?

Key you are using is invisible. Therefore, keep the TextButton/ImageButton invisible until the player has earned the badge. In both articles you attached the key line of code you’ll require is;

local success, hasBadge = pcall(BadgeService.UserHasBadgeAsync, BadgeService, player.UserId, badgeID)
-- The variable 'hasBadge' will either return true/false

Depending on what the pcall function returns, either true/false you can do a if statement.

if hasBadge then
 -- code here
end

Because you are having multiple badges, I’d suggest using a table to contain all of the ids and do something similar below,

local Badges = {
    0000000, -- Level 1
    0000000  -- Level 2
} -- Place IDs here!
for Index,BadgeID in ipairs(Badges) do

    local success, hasBadge = pcall(BadgeService.UserHasBadgeAsync, BadgeService, player.UserId, BadgeID)

    if hasBadge then
       -- code here (most likely a: .Visible = true/false)
       -- There are many ways of getting the Button such as renaming it to the badge ID, setting an attribute, etc. 
    end
end 

The code above is when the player joins the game or the character is loaded; however, if you’d like to update when a player earns a badge, simply set the Button that correlates to the Badge to be visible with the function that awards the player a badge. If you are having troubles with the UI resetting, you can disable ‘ResetOnSpawn.’

Let me know if you have any additional questions, but does this answer your question?

How would I get each badge individually and know which ones to make visible?

You insert ALL badge IDs into this table, you can find ALL of your badges attached to your game in studio!
Step 1)


Step 2)


The code I posted will help you out in acheiving your goal. The for loop will run through the table of badgeIDs and check which badges the player has been awarded. Based upon the result it returns (true/false) it can then make the Button Visible or not.

1 Like

Oh, I understand how the “for” works in this now! So if the badge is found, how would I connect the badge to the button in the “if hasBadge”?

As said, there are multiple ways you can do this. Because our loop is running through badgeIDs, we can rename the button to the BadgeID.

if hasBadge then
    if ScrollingFrame:FindFirstChild(tostring(BadgeID)) then
        ScrollingFrame:FindFirstChild(tostring(BadgeID)).Visible = true
    end
end

ScrollingFrame is an example of the parent of the button. This would look something like this;
98b8a0bcaec40b72081aff00454d9a01

The name of the ImageButton would just be renamed to the BadgeID, as shown above.

1 Like

I understand now. Thank you for helping me out. :slight_smile:

1 Like
game.Players.PlayerAdded:Connect(function(player)
local ScrollingFrame = script.Parent

local BadgeService = game:GetService("BadgeService")
local Badges = {
		2127226503, -- 1
		2127226519,  -- 2
		2127226537,  --3
		2127226542, --4
		2127245024, -- 5
		2127245129, --Bonus 1
		2127245139, -- Bonus 2
		2127245185 -- Bonus 3
} -- Place IDs here!
for Index,BadgeID in ipairs(Badges) do

	local success, hasBadge = pcall(BadgeService.UserHasBadgeAsync, BadgeService, player.UserId, BadgeID)

	if hasBadge then
		if ScrollingFrame:FindFirstChild(tostring(BadgeID)) then
			ScrollingFrame:FindFirstChild(tostring(BadgeID)).Visible = true
		end
		end
		end
		end)

I have this but it isn’t working. I have the script inside of the GUI with the buttons.

1 Like

There is no output information.

Instead of placing it in a PlayerAdded Event just have it run without it!

If this is within a LocalScript simply define the Local Player

2 Likes

It worked! Everything is functional now! Thank you so much!

1 Like