How Do I Make A Gui Appear To Only Players Who Own A Badge ?
For Example When A User Gains A Badge How Do I Make A Ui Appear Only For Them In Game For Example A Halo Gui To Put On A Exclusive Badge Rewarded Halo In Game . Or A Secret Teleporter For Badge Owners Only
It is rather simple. To find only the players who have the badge, you’ll need to use BadgeService, using BadgeService.UserHasBadgeAsync. Assume that Players.PlayerAdded was fired, you have a new Player for each event. You’ll use BadgeService.UserHasBadgeAsync using their user ID and then the badge ID respectively as arguments.
Note that the function requires pcall, a protected call, in case of failures which is similar to data store functions.
The function will return a boolean, thus you have to put the line in an if-else statement without having to compare anything. The code within the if statement should either:
Fire to the client to make the UI appear, if the UI is not significant in any way. In this method, those UI functionalities that fire to the server has to check whether the player is in a specific list of players that has a badge, and if not, do nothing. To create that list, every time BadgeService checks if someone has a badge or giving a player the badge, add them to that table.
Parent the UI from the server in a storage, maybe ServerStorage, if you don’t want sneaky clients trying to break your code and find that UI specifically. The UI has to be parented to Player.PlayerGui, if it was a ScreenGui.
Sorry for the bump, but I have a similar issue, where I am trying to make a button GUI only interactable if they own the badge. If they have the badge, they get teleported to a seperate place.
Put this as a Local Script in your button
(This enables or disables the button if the player has or doesn’t have a badge)
local badgeService = game:GetService("BadgeService") -- Gets the BadgeService
local BadgeID = 000000000 -- Your badge id
local player = game.Players.LocalPlayer -- Gets the current player
task.wait(1) -- Waits 1 second before starting
if badgeService:UserHasBadgeAsync(player.UserId, BadgeID) then -- If the player owns the badge
script.Parent.BackgroundColor3 = Color3.new(0.101961, 1, 0) -- This is to make sure the script ran fine, this can be deleted.
script.Parent.Active = true -- Makes button sink an input
script.Parent.Interactable = true -- Makes button interactable
else -- If the player does not own the badge
script.Parent.BackgroundColor3 = Color3.new(1, 0, 0.0156863) -- This is also to make sure the script ran fine, this can also be deleted.
script.Parent.Active = false -- Makes button NOT sink an input
script.Parent.Interactable = false -- Makes button NOT interactable
end
Your button should also have active and interactable turned off before starting the game.