PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

UserHasBadge

UserHasBadge checks whether a player owns a badge given the Player's Player/UserId|UserId and the badge’s id. Such a query can only be made under the following conditions:

  • This function must be called from the server, i.e. in a Script or ModuleScript eventually required by a Script.
  • The player in question must be present in the server.

Any badge for any game can be queried, no matter who created the badge or which game it is used for. There are a number of applications of UserHasBadge:

  • A restricted door that can only be opened by players who own a badge (see code sample)
  • A basic way of determining if a player has played another game
  • Very simple progress saving. However, it is recommended developers use DataStoreService for saving as it is more scale-able and robust (remember - players can delete their own badges).

Parameters

Name Type Default Description

The user ID of the user.

The badge ID of the badge.

Returns

Return Type Summary

True if the user has the badge.


Code Samples


BadgeService:UserHasBadge

The below example would print “The user has this badge” if a player joined whom owned an asset with an ID of 123. If the user didn’t own an asset with an ID of 123, it would print “The user does not have this badge.”

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)

Badge restricted door

This code sample creates a restricted door in the Workspace that can only be used by players who own the badge associated with BADGE_ID.

    local Players = game:GetService("Players")
    local BadgeService = game:GetService("BadgeService")
    
    local BADGE_ID = 1 -- change this
    
    -- create a door part
    local door = Instance.new("Part")
    door.Anchored = true 
    door.Size = Vector3.new(7, 10, 1)
    door.Position = Vector3.new(0, 5, 0)
    door.Parent = game.Workspace
    
    local debounce = false 
    
    door.Touched:Connect(function(hit)
    	if not debounce then
    		debounce = true
    		if hit then
    			local player = Players:GetPlayerFromCharacter(hit.Parent)
    			-- if player exists and owns the badge
    			if player and BadgeService:UserHasBadge(player.UserId, BADGE_ID) then
    				-- open the door
    				door.Transparency = 0.5
    				door.CanCollide = false 
    				-- wait
    				wait(3)
    				-- close the door
    				door.Transparency = 0
    				door.CanCollide = true
    			end
    		end
    		-- cooldown
    		wait(0.5)
    		debounce = false
    	end
    end)