BadgeService

Tampilkan yang Tidak Digunakan Lagi

*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.

Tidak Dapat Dibuat
Layanan

Kelas Layanan Lencana memberikan informasi dan fungsi yang terkait dengan lencana .Lencana digunakan di seluruh platform untuk mengenali pencapaian dan aktivitas pemain.Setelah memberikan lencana kepada pemain, itu ditambahkan ke inventaris mereka dan ditampilkan di halaman profil mereka.

Contoh Kode

The following example creates an awardBadge() function that handles potential errors that may occur when awarding a badge. Using properties of the badge fetched via BadgeService:GetBadgeInfoAsync(), it confirms that the badge can be awarded and does so using BadgeService:AwardBadge().

Awarding a Badge

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 0
local function awardBadge(player, badgeId)
-- Fetch badge information
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- Confirm that badge can be awarded
if badgeInfo.IsEnabled then
-- Award badge
local awardSuccess, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awardSuccess then
-- the AwardBadge function threw an error
warn("Error while awarding badge:", result)
elseif not result then
-- the AwardBadge function did not award a badge
warn("Failed to award badge.")
end
end
else
warn("Error while fetching badge info: " .. badgeInfo)
end
end
local function onPlayerAdded(player)
awardBadge(player, BADGE_ID)
end
Players.PlayerAdded:Connect(onPlayerAdded)

The following script waits for any player to enter the game and checks if they own a specific badge. This is useful for creating a restricted area with collision filtering or teleportation that only works if a player owns a special badge.

Checking Earned Badges

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 00000000 -- Change this to your badge ID
local function onPlayerAdded(player)
-- Check if the player has the badge
local success, hasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has badge!")
return
end
if hasBadge then
-- Handle player's badge ownership as needed
print(player.Name, "has badge", badgeId)
end
end
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)

This script checks which badges a user owns when they join the experience.

Checking Earned Badges in Batches

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeIds = { 0000000000, 1111111111, 2222222222 } -- Change this to a list of your badge IDs
local function onPlayerAdded(player)
-- Check if the player has any of the badges
local success, result = pcall(function()
return BadgeService:CheckUserBadgesAsync(player.UserId, badgeIds)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player", player.Name, "has badges:", result)
return
end
local ownedBadgeIds = result
if #ownedBadgeIds == 0 then
print(player.Name, "does not have any of the badges")
else
print(player.Name, "has the following badges:", table.concat(ownedBadgeIds, ", "))
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

This sample prints badge information fetched via BadgeService:GetBadgeInfoAsync().

Getting Badge Info

local BadgeService = game:GetService("BadgeService")
-- Fetch badge information
local success, result = pcall(function()
return BadgeService:GetBadgeInfoAsync(00000000) -- Change this to desired badge ID
end)
-- Output the information
if success then
print("Badge:", result.Name)
print("Enabled:", result.IsEnabled)
print("Description:", result.Description)
print("Icon:", "rbxassetid://" .. result.IconImageId)
else
warn("Error while fetching badge info:", result)
end

Rangkuman

Metode

Properti

Metode

AwardBadge

Hasil

Memberikan Player sebuah lencana dengan UserId dan ID lencana.Batas tingkat: 50 + 35 * [jumlah pengguna] per menit .Untuk memberikan penghargaan badge dengan sukses:

  • Pemain harus saat ini terhubung ke game.
  • Pemain tidak boleh sudah memiliki lencana (perhatikan bahwa pemain dapat menghapus lencana yang diberikan dari profil mereka dan diberikan lencana lagi).
  • Lencana harus diberikan dari sisi server Script atau dari ModuleScript akhirnya diperlukan oleh Script , bukan dari LocalScript .
  • Lencana harus diberikan di tempat yang merupakan bagian dari permainan yang terkait dengan lencana.
  • Lencana harus diaktifkan; periksa ini menggunakan properti IsEnabled dari lencana yang diambil melalui BadgeService:GetBadgeInfoAsync() .

Lihat juga:

Parameter

userId: number

The Player.UserId dari pengguna badge yang akan diberikan kepada.

Nilai Default: ""
badgeId: number

ID lencana yang akan diberikan.

Nilai Default: ""

Memberikan nilai

Boolean dari true jika lencana diberikan dengan sukses.

Contoh Kode

The following example creates an awardBadge() function that handles potential errors that may occur when awarding a badge. Using properties of the badge fetched via BadgeService:GetBadgeInfoAsync(), it confirms that the badge can be awarded and does so using BadgeService:AwardBadge().

Awarding a Badge

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 0
local function awardBadge(player, badgeId)
-- Fetch badge information
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- Confirm that badge can be awarded
if badgeInfo.IsEnabled then
-- Award badge
local awardSuccess, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awardSuccess then
-- the AwardBadge function threw an error
warn("Error while awarding badge:", result)
elseif not result then
-- the AwardBadge function did not award a badge
warn("Failed to award badge.")
end
end
else
warn("Error while fetching badge info: " .. badgeInfo)
end
end
local function onPlayerAdded(player)
awardBadge(player, BADGE_ID)
end
Players.PlayerAdded:Connect(onPlayerAdded)

CheckUserBadgesAsync

Hasil

Memeriksa daftar ID lencana melawan UserId dan kembali daftar ID lencana yang dimiliki pemain.

Metode ini mendukung batch hingga 10 lencana. Gunakan BadgeService:UserHasBadgeAsync() untuk pencarian lencana tunggal.

Batas tingkat: 10 + 5 * [jumlah pemain] per menit di setiap server.

Setiap kumpulan lencana untuk pengalaman apa pun dapat dipertanyakan, tidak peduli siapa yang membuat lencana atau pengalaman apa yang mereka untuk.Setiap UserId dapat digunakan dalam Script , tetapi di LocalScript , hanya UserId pengguna lokal yang kliennya menjalankan skrip dapat digunakan.

Parameter

userId: number

The UserId dari pemain untuk memeriksa kepemilikan lencana yang ditentukan.

Nilai Default: ""
badgeIds: Array

Daftar ID dari lencana untuk memeriksa kepemilikan. Panjang maksimum 10.

Nilai Default: ""

Memberikan nilai

Daftar ID lencana yang dimiliki pengguna yang diberikan keluar dari ID lencana yang disediakan.Kosong jika tidak ada lencana yang disediakan dimiliki oleh pengguna yang diberikan.Tidak dijamin berada di urutan yang sama dengan daftar input.

Contoh Kode

This script checks which badges a user owns when they join the experience.

Checking Earned Badges in Batches

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeIds = { 0000000000, 1111111111, 2222222222 } -- Change this to a list of your badge IDs
local function onPlayerAdded(player)
-- Check if the player has any of the badges
local success, result = pcall(function()
return BadgeService:CheckUserBadgesAsync(player.UserId, badgeIds)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player", player.Name, "has badges:", result)
return
end
local ownedBadgeIds = result
if #ownedBadgeIds == 0 then
print(player.Name, "does not have any of the badges")
else
print(player.Name, "has the following badges:", table.concat(ownedBadgeIds, ", "))
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

GetBadgeInfoAsync

Hasil

Fungsi ini mengambil informasi tentang lencana dengan memberikan ID-nya.Dibutuhkan sedikit waktu untuk memuat informasi dari situs web Roblox; panggilan berulang akan disimpan untuk waktu yang singkat.Ini mengembalikan kamus dengan bidang berikut:


<th>Jenis</th>
<th>Deskripsi</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Nama</b></td>
<td>string</td>
<td>Nama lencana.</td>
</tr>
<tr>
<td><b>Deskripsi</b></td>
<td>string</td>
<td>Deskripsi lencana.</td>
</tr>
<tr>
<td><b>ID Gambar Ikon</b></td>
<td>int64</td>
<td>ID aset dari gambar untuk lencana.</td>
</tr>
<tr>
<td><b>Diaktifkan</b></td>
<td>bool</td>
<td>Menunjukkan apakah lencana tersedia untuk diberikan.</td>
</tr>
</tbody>
Kunci

Lihat juga:

Parameter

badgeId: number

ID lencana dari lencana yang informasinya harus diambil.

Nilai Default: ""

Memberikan nilai

Kamus informasi tentang lencana yang ditentukan.

Contoh Kode

This sample prints badge information fetched via BadgeService:GetBadgeInfoAsync().

Getting Badge Info

local BadgeService = game:GetService("BadgeService")
-- Fetch badge information
local success, result = pcall(function()
return BadgeService:GetBadgeInfoAsync(00000000) -- Change this to desired badge ID
end)
-- Output the information
if success then
print("Badge:", result.Name)
print("Enabled:", result.IsEnabled)
print("Description:", result.Description)
print("Icon:", "rbxassetid://" .. result.IconImageId)
else
warn("Error while fetching badge info:", result)
end

UserHasBadgeAsync

Hasil

Memeriksa dan mengembalikan apakah Player memiliki lencana yang diberikan karena UserId dan ID lencana.Batas tingkat: 50 + 35 * [jumlah pemain] per menit .Anda dapat memanggil fungsi dari server dalam Script atau ModuleScript akhirnya diperlukan oleh Script , dan pengguna dalam pertanyaan harus hadir di server agar query dapat dijalankan.Saat memanggil metode dari klien dalam LocalScript , hanya berfungsi untuk pengguna lokal yang kliennya menjalankan skrip.

Setiap lencana untuk permainan apa pun dapat dipertanyakan, tidak peduli siapa yang membuat lencana atau untuk pengalaman apa itu digunakan.

Lihat juga:

Parameter

userId: number

The Player.UserId dari pemain untuk memeriksa kepemilikan lencana yang ditentukan.

Nilai Default: ""
badgeId: number

ID lencana dari lencana yang kepemilikannya akan diperiksa.

Nilai Default: ""

Memberikan nilai

Menunjukkan apakah pengguna yang ditentukan memiliki lencana yang ditentukan.

Contoh Kode

The following script waits for any player to enter the game and checks if they own a specific badge. This is useful for creating a restricted area with collision filtering or teleportation that only works if a player owns a special badge.

Checking Earned Badges

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 00000000 -- Change this to your badge ID
local function onPlayerAdded(player)
-- Check if the player has the badge
local success, hasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has badge!")
return
end
if hasBadge then
-- Handle player's badge ownership as needed
print(player.Name, "has badge", badgeId)
end
end
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)

Acara