BadgeService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
คลาส BadgeService ให้ข้อมูลและฟังก์ชันที่เกี่ยวข้องกับ เหรียญตราสัญลักษณ์ถูกใช้ทั่วทั้งแพลตฟอร์มเพื่อระบุความสำเร็จและกิจกรรมของผู้เล่นเมื่อมอบตราให้กับผู้เล่นแล้ว จะถูกเพิ่มลงในสินค้าคงคลังของพวกเขาและแสดงบนหน้าโปรไฟล์ของพวกเขา
ตัวอย่างโค้ด
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().
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.
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.
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().
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
สรุป
วิธีการ
มอบตราให้กับผู้เล่นโดยให้รหัสของแต่ละคน
ตรวจสอบรายการของรหัสป้ายกำกับกับ UserId และส่งคืนรายการรหัสป้ายกำกับที่ผู้เล่นเป็นเจ้าของ
ดึงข้อมูลเกี่ยวกับตราที่มอบรหัสไปยังมัน
ตรวจสอบว่าผู้เล่นมีตราหรือไม่โดยให้รหัสตรา Player.UserId และรหัสตรา
คุณสมบัติ
วิธีการ
AwardBadge
ให้ตรา Player ที่มีรหัส UserId และรหัสตราขีดจำกัดอัตรา: 50 + 35 * [จํานวนผู้ใช้] ต่อนาที .เหรียญตรา:
- ผู้เล่นต้องเชื่อมต่อกับเกมในปัจจุบัน
- ผู้เล่นไม่ควรมีตราแล้ว (โปรดทราบว่าผู้เล่นอาจลบตราที่ได้รับจากโปรไฟล์และได้รับตราอีกครั้ง)
- ตราต้องได้รับจากฝั่งเซิร์ฟเวอร์ Script หรือจาก ModuleScript ในที่สุดที่จำเป็นโดย Script ไม่ใช่จาก LocalScript
- เหรียญตรา
- ตราต้องเปิดใช้งาน; ตรวจสอบนี้โดยใช้คุณสมบัติ IsEnabled ของตราที่ดึงผ่าน BadgeService:GetBadgeInfoAsync()
ดูเพิ่ม:
พารามิเตอร์
The Player.UserId ของผู้ใช้ที่ตราจะถูกมอบให้
รหัสของตราที่จะได้รับรางวัล
ส่งค่ากลับ
บูลีนของ true หากได้รับตราสัญลักษณ์สําเร็จ
ตัวอย่างโค้ด
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().
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
ตรวจสอบรายการของรหัสป้ายกำกับกับ UserId และส่งคืนรายการรหัสป้ายกำกับที่ผู้เล่นเป็นเจ้าของ
วิธีนี้สนับสนุนล็อตของเหรียญถึง 10 เหรียญ ใช้ BadgeService:UserHasBadgeAsync() สำหรับการค้นหาเหรียญเดียว
ขีดจํากัดอัตรา: 10 + 5 * [จํานวนผู้เล่น] ต่อนาที ในแต่ละเซิร์ฟเวอร์
สามารถค้นหาชุดเหรียญตราสําหรับประสบการณ์ใดก็ได้ ไม่ว่าใครจะสร้างเหรียญตราหรือสําหรับประสบการณ์ใดใดๆ UserId สามารถใช้ใน Script แต่ใน LocalScript เฉพาะ UserId ของผู้ใช้ท้องถิ่นที่คลายของเครื่องลูกค้ากำลังดำเนินการสคริปต์สามารถใช้ได้
พารามิเตอร์
The UserId ของผู้เล่นที่จะตรวจสอบสิทธิ์การเป็นเจ้าของของตราสัญลักษณ์ที่ระบุ
รายการรหัสของตราสัญลักษณ์ที่ตรวจสอบสิทธิ์การเป็นเจ้าของ ความยาวสูงสุดคือ 10
ส่งค่ากลับ
รายการของรหัสตราที่ผู้ใช้ที่กำหนดให้เป็นเจ้าของออกจากรหัสตราที่ให้ไว้ว่างเปล่าหากตราที่ให้ไว้ไม่มีใครเป็นเจ้าของโดยผู้ใช้ที่กำหนดไม่รับประกันว่าจะอยู่ในลำดับเดียวกับรายการอินพุต
ตัวอย่างโค้ด
This script checks which badges a user owns when they join the experience.
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
ฟังก์ชันนี้รับข้อมูลเกี่ยวกับตราที่ให้ ID ของมันใช้เวลาสั้น ๆ ในการโหลดข้อมูลจากเว็บไซต์ Roblox; การโทรซ้ำจะแคชไว้เป็นระยะเวลาสั้น ๆมันจะคืนสารานุกรมที่มีฟิลด์ต่อไปนี้:
<th>ประเภท</th><th>คําอธิบาย</th></tr></thead><tbody><tr><td><b>ชื่อ</b></td><td>สตริง</td><td>เหรียญตรา</td></tr><tr><td><b>คําอธิบาย</b></td><td>สตริง</td><td>เหรียญตรา</td></tr><tr><td><b>ไอคอนรูปภาพID</b></td><td>ตัวเลข 64 บิต</td><td>ID สินทรัพย์ของภาพสำหรับตราสัญลักษณ์</td></tr><tr><td><b>เปิดใช้งานแล้ว</b></td><td>บูล</td><td>บ่งบอกว่าตราสัญลักษณ์มีให้มอบหรือไม่</td></tr></tbody>
กุญแจ |
---|
ดูเพิ่ม:
พารามิเตอร์
รหัสประกาศของประกาศที่ข้อมูลควรได้รับ
ส่งค่ากลับ
เหรียญตรา
ตัวอย่างโค้ด
This sample prints badge information fetched via BadgeService:GetBadgeInfoAsync().
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
ตรวจสอบและส่งคืนว่า Player มีตราสัญลักษณ์หรือไม่โดยใช้ UserId และรหัสตราสัญลักษณ์ขีดจำกัดอัตรา: 50 + 35 * [จํานวนผู้เล่น] ต่อนาที .คุณสามารถเรียกฟังก์ชันจากเซิร์ฟเวอร์ใน Script หรือ ModuleScript ในที่สุดที่จำเป็นโดย Script และผู้ใช้ในคำถามจะต้องอยู่ในเซิร์ฟเวอร์เพื่อให้คำถามทำงานเมื่อเรียกวิธีจากไคลเอนต์ใน LocalScript จะทำงานได้เฉพาะสำหรับผู้ใช้ท้องถิ่นที่ไคลเอนต์ของเขากําลังดําเนินการตามสคริปต์
ตราใดๆ สำหรับเกมใดๆ สามารถสอบถามได้ ไม่ว่าใครจะสร้างตราหรือใช้ประสบการณ์สำหรับตรานั้น
ดูเพิ่ม:
พารามิเตอร์
The Player.UserId เหรียญตรา
ID ตราของตราที่จะตรวจสอบสิทธิ์การเป็นเจ้าของ
ส่งค่ากลับ
เหรียญตรา
ตัวอย่างโค้ด
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.
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)