Engine Class
BadgeService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
วิธีการ
AwardBadge(userId: User,badgeId: number):boolean |
AwardBadgeAsync(userId: User,badgeId: number):boolean |
CheckUserBadgesAsync(userId: User,badgeIds: {any}):{any} |
GetBadgeInfoAsync(badgeId: number):Dictionary |
IsDisabled(badgeId: number):boolean |
UserHasBadge(userId: User,badgeId: number):boolean |
UserHasBadgeAsync(userId: User,badgeId: number):boolean |
เอกสารอ้างอิงเกี่ยวกับ API
วิธีการ
AwardBadge
AwardBadgeAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
การมอบเหรียญ
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 0
local function awardBadge(player, badgeId)
-- ดึงข้อมูลเข็มกลัด
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- ยืนยันว่าเข็มกลัดสามารถมอบได้
if badgeInfo.IsEnabled then
-- มอบเข็มกลัด
local awarded, errorMessage = pcall(BadgeService.AwardBadgeAsync, BadgeService, player.UserId, badgeId)
if not awarded then
warn("เกิดข้อผิดพลาดขณะมอบเข็มกลัด:", errorMessage)
end
end
else
warn("เกิดข้อผิดพลาดขณะดึงข้อมูลเข็มกลัด: " .. badgeInfo)
end
end
local function onPlayerAdded(player)
awardBadge(player, BADGE_ID)
end
Players.PlayerAdded:Connect(onPlayerAdded)CheckUserBadgesAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบเหรียญที่ได้รับในกลุ่ม
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeIds = { 0000000000, 1111111111, 2222222222 } -- เปลี่ยนเป็นรายการ ID รางวัลของคุณ
local function onPlayerAdded(player)
-- ตรวจสอบว่าผู้เล่นมีรางวัลใด ๆ หรือไม่
local success, result = pcall(function()
return BadgeService:CheckUserBadgesAsync(player.UserId, badgeIds)
end)
-- หากมีข้อผิดพลาด ให้แสดงคำเตือนและออกจากฟังก์ชัน
if not success then
warn("เกิดข้อผิดพลาดขณะตรวจสอบว่าผู้เล่น", player.Name, "มีรางวัล:", result)
return
end
local ownedBadgeIds = result
if #ownedBadgeIds == 0 then
print(player.Name, "ไม่มีรางวัลใด ๆ")
else
print(player.Name, "มีรางวัลดังต่อไปนี้:", table.concat(ownedBadgeIds, ", "))
end
end
Players.PlayerAdded:Connect(onPlayerAdded)GetBadgeInfoAsync
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
การรับข้อมูลเหรียญ
local BadgeService = game:GetService("BadgeService")
-- ดึงข้อมูลเหรียญ
local success, result = pcall(function()
return BadgeService:GetBadgeInfoAsync(00000000) -- เปลี่ยนหมายเลขนี้เป็น ID เหรียญที่ต้องการ
end)
-- แสดงข้อมูล
if success then
print("เหรียญ:", result.Name)
print("เปิดใช้งาน:", result.IsEnabled)
print("คำอธิบาย:", result.Description)
print("ไอคอน:", "rbxassetid://" .. result.IconImageId)
else
warn("เกิดข้อผิดพลาดระหว่างการดึงข้อมูลเหรียญ:", result)
endIsDisabled
IsLegal
UserHasBadge
UserHasBadgeAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบเหรียญที่ได้รับ
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 00000000 -- เปลี่ยนให้เป็น ID ของเหรียญของคุณ
local function onPlayerAdded(player)
-- ตรวจสอบว่าผู้เล่นมีเหรียญนี้หรือไม่
local success, hasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
end)
-- หากมีข้อผิดพลาด ให้เตือนและออกจากฟังก์ชัน
if not success then
warn("เกิดข้อผิดพลาดขณะตรวจสอบว่าผู้เล่นมีเหรียญหรือไม่!")
return
end
if hasBadge then
-- จัดการความเป็นเจ้าของเหรียญของผู้เล่นตามที่ต้องการ
print(player.Name, "มีเหรียญ", badgeId)
end
end
-- เชื่อมต่อเหตุการณ์ "PlayerAdded" กับฟังก์ชัน "onPlayerAdded()"
Players.PlayerAdded:Connect(onPlayerAdded)