Engine Class
MarketplaceService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
วิธีการ
เหตุการณ์
PromptBulkPurchaseFinished(player: Instance,status: Enum.MarketplaceBulkPurchasePromptStatus,results: Dictionary):RBXScriptSignal |
PromptBundlePurchaseFinished(player: Instance,bundleId: number,wasPurchased: boolean):RBXScriptSignal |
PromptGamePassPurchaseFinished(player: Instance,gamePassId: number,wasPurchased: boolean):RBXScriptSignal |
PromptProductPurchaseFinished(userId: number,productId: number,isPurchased: boolean):RBXScriptSignal |
PromptPurchaseFinished(player: Instance,assetId: number,isPurchased: boolean):RBXScriptSignal |
PromptRobloxSubscriptionPurchaseFinished(user: Player,didTryPurchasing: boolean):RBXScriptSignal |
PromptSubscriptionPurchaseFinished(user: Player,subscriptionId: string,didTryPurchasing: boolean):RBXScriptSignal |
Callbacks
ProcessReceipt(receiptInfo: Dictionary):Enum.ProductPurchaseDecision |
เอกสารอ้างอิงเกี่ยวกับ API
วิธีการ
BindReceiptHandler
MarketplaceService:BindReceiptHandler(
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
MarketplaceService:BindReceiptHandler
-- หมายเหตุ: หากตัวจัดการของคุณมอบผลประโยชน์ถาวร (สกุลเงิน, รายการ) ให้ใช้
-- DataStoreService:UpdateAsync() กับ TransferRequestId เพื่อป้องกัน
-- การมอบซ้ำเมื่อใบเสร็จเดียวกันถูกส่งไปยังเซิร์ฟเวอร์หลายตัว
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
-- จัดการใบเสร็จของผู้ส่ง (ผู้เล่นที่ส่ง Robux)
MarketplaceService:BindReceiptHandler(
Enum.ReceiptType.RobuxTransferSender,
function(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
-- ผู้เล่นไม่ได้อยู่ในเซิร์ฟเวอร์นี้; เลื่อนเพื่อให้ใบเสร็จ
-- ถูกส่งใหม่ไปยังเซิร์ฟเวอร์ที่พวกเขาอยู่ตอนนี้
return Enum.ReceiptDecision.NotProcessedYet
end
print(
`{player.Name} ได้ส่ง {receiptInfo.CurrencySpent} Robux`
.. ` (TransferRequestId: {receiptInfo.TransferRequestId})`
)
-- มอบการยืนยันของผู้ส่งหรืออัปเดต UI ที่นี่
return Enum.ReceiptDecision.Processed
end
)
-- จัดการใบเสร็จของผู้รับ (ผู้เล่นที่ได้รับ Robux)
MarketplaceService:BindReceiptHandler(
Enum.ReceiptType.RobuxTransferReceiver,
function(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ReceiptDecision.NotProcessedYet
end
print(
`{player.Name} ได้รับ {receiptInfo.CurrencySpent} Robux`
.. ` (TransferRequestId: {receiptInfo.TransferRequestId})`
)
-- มอบผลประโยชน์ของผู้รับหรืออัปเดต UI ที่นี่
return Enum.ReceiptDecision.Processed
end
)GetDeveloperProductsAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
MarketplaceService:GetDeveloperProductsAsync
local MarketplaceService = game:GetService("MarketplaceService")
local developerProducts = MarketplaceService:GetDeveloperProductsAsync():GetCurrentPage()
for _, developerProduct in pairs(developerProducts) do
for field, value in pairs(developerProduct) do
print(field .. ": " .. value)
end
print(" ")
endGetProductInfo
GetProductInfoAsync
พารามิเตอร์
| ค่าเริ่มต้น: "Asset" |
ส่งค่ากลับ
ตัวอย่างโค้ด
การดึงข้อมูลผลิตภัณฑ์
local MarketplaceService = game:GetService("MarketplaceService")
local ASSET_ID = 125378389
local asset = MarketplaceService:GetProductInfoAsync(ASSET_ID)
print(asset.Name .. " :: " .. asset.Description)แสดงส่วนลดราคา
local MarketplaceService = game:GetService("MarketplaceService")
local PASS_ID = 12345678
local textLabel = script.Parent
local DiscountTypeDisplay = {
RobloxPlusSubscription = "ส่วนลด Roblox Plus",
}
local productInfo = MarketplaceService:GetProductInfoAsync(PASS_ID, Enum.InfoType.GamePass)
print(string.format("ราคาเดิม: %d", productInfo.UserBasePriceInRobux))
for _, discount in ipairs(productInfo.PriceDiscountDetails) do
local displayName = DiscountTypeDisplay[discount.Type] or "ส่วนลดอื่นๆ"
print(string.format("%s (%d%%): -%d", displayName, discount.Percent, discount.AmountInRobux))
end
print(string.format("คุณต้องจ่าย: %d", productInfo.PriceInRobux))แสดงตัวเลือกที่มีอยู่ตามเวลา
local MarketplaceService = game:GetService("MarketplaceService")
local info = MarketplaceService:GetProductInfoAsync(105589844216517, Enum.InfoType.Asset)
if info.TimedOptions then
for _, option in info.TimedOptions do
local days = option.Duration / 86400
print(string.format("%d วัน - %d โรบัค", days, option.Price))
end
endGetRobloxSubscriptionDetailsAsync
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบรายละเอียดการสมัครสมาชิก Roblox
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local success, details = pcall(function()
return MarketplaceService:GetRobloxSubscriptionDetailsAsync(player)
end)
if success and details.IsSubscribed then
-- 1. ตรวจสอบความภักดี (เช่น, สมัครสมาชิกเกิน 60 วัน)
local threeMonths = 60 * 24 * 60 * 60
if details.StartTime and (os.time() - details.StartTime.UnixTimestamp) > threeMonths then
print("มอบ 'สกินสมาชิก 3 เดือน' ให้!")
end
-- 2. ตรวจสอบการเข้าถึง
if details.IsOriginExperience then
print("การเข้าถึงได้รับการยืนยัน: ผู้ใช้สมัครสมาชิกผ่านประสบการณ์นี้.")
else
print("ผู้ใช้สมัครสมาชิกที่อื่น (เว็บไซต์หรือประสบการณ์อื่น).")
end
end
end)GetSubscriptionProductInfoAsync
พารามิเตอร์
ส่งค่ากลับ
GetUsersPriceLevelsAsync
พารามิเตอร์
ส่งค่ากลับ
{PriceLevelInfo}
ตัวอย่างโค้ด
รับระดับราคา สำหรับรายการของผู้ใช้
-- รับบริการ MarketplaceService
local MarketplaceService = game:GetService("MarketplaceService")
-- กำหนดฟังก์ชันเพื่อดึงระดับราคา สำหรับรายชื่อผู้ใช้
local function getPriceLevels(userIds)
local success, result = pcall(function()
return MarketplaceService:GetUsersPriceLevelsAsync(userIds)
end)
if success then
-- แมพแต่ละ PriceLevelInfo ไปยังตารางค้นหา UserId -> PriceLevel
local lookup = {}
for _, info in ipairs(result) do
lookup[info.UserId] = info.PriceLevel
end
return lookup
else
warn("เกิดข้อผิดพลาดในการดึงระดับราคา:", result)
return nil
end
end
-- ตัวอย่างที่ใช้รหัสผู้ใช้ตัวอย่าง
local user1Id = 123456789
local user2Id = 987654321
-- เรียกฟังก์ชันและเก็บผลลัพธ์
local priceLevels = getPriceLevels({user1Id, user2Id})
-- หากสำเร็จ ให้พิมพ์ระดับของแต่ละผู้ใช้
if priceLevels then
print("ระดับราคา สำหรับผู้ใช้ 1:", priceLevels[user1Id])
print("ระดับราคา สำหรับผู้ใช้ 2:", priceLevels[user2Id])
else
print("ไม่สามารถดึงระดับราคาได้.")
endGetUserSubscriptionDetailsAsync
ส่งค่ากลับ
GetUserSubscriptionPaymentHistoryAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
MarketplaceService:GetUserSubscriptionPaymentHistoryAsync
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local SUBSCRIPTION_ID = "EXP-0"
local function checkSubscriptionHistory(player: Player)
local subscriptionHistory = {}
local success, err = pcall(function()
subscriptionHistory = MarketplaceService:GetUserSubscriptionPaymentHistoryAsync(player, SUBSCRIPTION_ID)
end)
if not success then
warn(`เกิดข้อผิดพลาดขณะตรวจสอบประวัติสมาชิก: {err}`)
return
end
if next(subscriptionHistory) then
-- ผู้ใช้มีประวัติสมาชิกบางส่วนในช่วง 12 เดือนที่ผ่านม.
-- พิมพ์รายละเอียดของแต่ละรายการชำระเงินจากประวัติสมาชิก.
print(`ผู้เล่น {player.Name} เคยสมัครสมาชิก {SUBSCRIPTION_ID} เมื่อก่อน:`)
for entryNum, paymentEntry in subscriptionHistory do
local paymentStatus = tostring(paymentEntry.PaymentStatus)
local cycleStartTime = paymentEntry.CycleStartTime:FormatLocalTime("LLL", "en-us")
local cycleEndTime = paymentEntry.CycleEndTime:FormatLocalTime("LLL", "en-us")
print(`{entryNum}: {paymentStatus} ({cycleStartTime} - {cycleEndTime})`)
end
else
print(`ผู้เล่น {player.Name} ไม่เคยสมัครสมาชิก {SUBSCRIPTION_ID} เมื่อก่อน.`)
end
end
-- เรียก checkSubscriptionHistory สำหรับผู้เล่นที่อยู่ในเกมแล้ว
for _, player in ipairs(Players:GetPlayers()) do
checkSubscriptionHistory(player)
end
-- เรียก checkSubscriptionHistory สำหรับผู้เล่นในอนาคตทั้งหมด
Players.PlayerAdded:Connect(checkSubscriptionHistory)GetUserSubscriptionStatusAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบสถานะการสมัครสมาชิกของผู้ใช้
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local subscriptionID = "EXP-00000"
local function checkSubStatus(player)
local subStatus = {}
local success, message = pcall(function()
-- คืนค่า IsRenewing และ IsSubscribed
subStatus = MarketplaceService:GetUserSubscriptionStatusAsync(player, subscriptionID)
end)
if not success then
warn("เกิดข้อผิดพลาดขณะตรวจสอบว่าผู้เล่นมีการสมัครสมาชิกหรือไม่: " .. tostring(message))
return
end
if subStatus["IsSubscribed"] then
print(player.Name .. " สมัครสมาชิกอยู่ด้วย " .. subscriptionID)
-- มอบสิทธิ์ที่เกี่ยวข้องกับการสมัครสมาชิกให้กับผู้เล่น
end
end
Players.PlayerAdded:Connect(checkSubStatus)PlayerOwnsAsset
PlayerOwnsAssetAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบความเป็นเจ้าของไอเทม
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
-- สิ่งของที่เรากำลังตรวจสอบ: https://www.roblox.com/catalog/30331986/Midnight-Shades
local ASSET_ID = 30331986
local ASSET_NAME = "Midnight Shades"
local function onPlayerAdded(player)
local success, doesPlayerOwnAsset =
pcall(MarketplaceService.PlayerOwnsAssetAsync, MarketplaceService, player, ASSET_ID)
if not success then
local errorMessage = doesPlayerOwnAsset
warn(`เกิดข้อผิดพลาดในการตรวจสอบว่าผู้เล่น {player.Name} เป็นเจ้าของ {ASSET_NAME}: {errorMessage}`)
return
end
if doesPlayerOwnAsset then
print(`ผู้เล่น {player.Name} เป็นเจ้าของ {ASSET_NAME}`)
else
print(`ผู้เล่น {player.Name} ไม่เป็นเจ้าของ {ASSET_NAME}`)
end
end
Players.PlayerAdded:Connect(onPlayerAdded)PlayerOwnsBundle
PlayerOwnsBundleAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบความเป็นเจ้าของแบนเดิล
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
-- ชุดที่เรากำลังตรวจสอบ: https://www.roblox.com/bundles/589/Junkbot
local BUNDLE_ID = 589
local BUNDLE_NAME = "Junkbot"
Players.PlayerAdded:Connect(function(player)
local success, doesPlayerOwnBundle = pcall(function()
return MarketplaceService:PlayerOwnsBundleAsync(player, BUNDLE_ID)
end)
if success == false then
print("การเรียก PlayerOwnsBundleAsync ล้มเหลว: ", doesPlayerOwnBundle)
return
end
if doesPlayerOwnBundle then
print(player.Name .. " เป็นเจ้าของ " .. BUNDLE_NAME)
else
print(player.Name .. " ไม่เป็นเจ้าของ " .. BUNDLE_NAME)
end
end)PromptBulkPurchase
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ลูกค้าเพื่อการซื้อจำนวนมากที่ถูกกระตุ้น
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local promptBulkPurchaseEvent = ReplicatedStorage:WaitForChild("PromptBulkPurchaseEvent")
local part = Instance.new("Part")
part.Parent = workspace
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
clickDetector.MouseClick:Connect(function()
promptBulkPurchaseEvent:FireServer({
{ Type = Enum.MarketplaceProductType.AvatarAsset, Id = "16630147" },
{ Type = Enum.MarketplaceProductType.AvatarBundle, Id = "182" },
})
end)เซิร์ฟเวอร์สั่งซื้อจำนวนมาก
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local promptBulkPurchaseEvent = Instance.new("RemoteEvent")
promptBulkPurchaseEvent.Name = "PromptBulkPurchaseEvent"
promptBulkPurchaseEvent.Parent = ReplicatedStorage
--ฟังการเกิดเหตุการณ์ RemoteEvent จาก Client และจากนั้นเรียกพรอมต์สั่งซื้อจำนวนมาก
promptBulkPurchaseEvent.OnServerEvent:Connect(function(player, items)
MarketplaceService:PromptBulkPurchase(player, items, {})
end)การเสนอให้ซื้อทางเลือกแบบมีเวลา
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ASSET_ID = 105589844216517
Players.PlayerAdded:Connect(function(player)
local info = MarketplaceService:GetProductInfoAsync(ASSET_ID, Enum.InfoType.Asset)
if info.TimedOptions and #info.TimedOptions > 0 then
-- ค้นหาตัวเลือกที่มีระยะเวลาสั้นที่สุด
local shortest = info.TimedOptions[1]
for _, option in info.TimedOptions do
if option.Duration < shortest.Duration then
shortest = option
end
end
local items = {
{
Type = Enum.MarketplaceProductType.AvatarAsset,
Id = tostring(ASSET_ID),
PurchaseOptions = {
{ Type = Enum.PurchaseOption.TimedOption, Value = shortest.Duration },
},
}
}
MarketplaceService:PromptBulkPurchase(player, items, {})
end
end)PromptBundlePurchase
PromptCancelSubscription
PromptGamePassPurchase
PromptPremiumPurchase
PromptProductPurchase
MarketplaceService:PromptProductPurchase(
):()
พารามิเตอร์
| ค่าเริ่มต้น: true |
| ค่าเริ่มต้น: "Default" |
ส่งค่ากลับ
()
ตัวอย่างโค้ด
MarketplaceService:PromptProductPurchase
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local productId = 0000000 -- เปลี่ยนนี้เป็น ID ของผลิตภัณฑ์นักพัฒนาของคุณ
-- ฟังก์ชันเพื่อเรียกซื้อผลิตภัณฑ์นักพัฒนา
local function promptPurchase()
MarketplaceService:PromptProductPurchase(player, productId)
end
promptPurchase()PromptPurchase
MarketplaceService:PromptPurchase(
):()
พารามิเตอร์
| ค่าเริ่มต้น: true |
| ค่าเริ่มต้น: "Default" |
ส่งค่ากลับ
()
ตัวอย่างโค้ด
LocalScript (ลูกค้า)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local promptPurchaseEvent = ReplicatedStorage:WaitForChild("PromptPurchaseEvent")
local part = Instance.new("Part")
part.Parent = workspace
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
clickDetector.MouseClick:Connect(function()
promptPurchaseEvent:FireServer(16630147)
end)สคริปต์ (เซิร์ฟเวอร์)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local promptPurchaseEvent = Instance.new("RemoteEvent")
promptPurchaseEvent.Name = "PromptPurchaseEvent"
promptPurchaseEvent.Parent = ReplicatedStorage
-- ฟัง RemoteEvent เพื่อให้ส่งมาจากไคลเอนต์ แล้วเรียกใช้การกระตุ้นการซื้อ
promptPurchaseEvent.OnServerEvent:Connect(function(player, id)
MarketplaceService:PromptPurchase(player, id)
end)PromptRobloxSubscriptionPurchase
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ซื้อสมัครสมาชิก Roblox Plus
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local teleporter = script.Parent
local showModal = true
local EXCLUSIVE_AREA_POSITION = Vector3.new(1200, 200, 60)
-- มอบรางวัลและเทเลพอร์ตผู้เล่นที่สมัครสมาชิกไปยังพื้นที่พิเศษ
local function grantRewardAndTeleport(player)
player:RequestStreamAroundAsync(EXCLUSIVE_AREA_POSITION)
local character = player.Character
if character and character.Parent then
local currentPivot = character:GetPivot()
character:PivotTo(currentPivot * CFrame.new(EXCLUSIVE_AREA_POSITION))
end
end
-- ตรวจจับส่วนของอักขระที่สัมผัสกับเทเลพอร์ต
teleporter.Touched:Connect(function(otherPart)
local player = Players:GetPlayerFromCharacter(otherPart.Parent)
if not player then
return
end
if not player:GetAttribute("CharacterPartsTouching") then
player:SetAttribute("CharacterPartsTouching", 0)
end
player:SetAttribute("CharacterPartsTouching", player:GetAttribute("CharacterPartsTouching") + 1)
if player.HasRobloxSubscription then
-- ผู้เล่นมี Roblox Plus อยู่แล้ว; มอบรางวัลทันที
grantRewardAndTeleport(player)
else
-- เรียกร้องการสมัครสมาชิก Roblox Plus, บล็อกครั้งเดียวทุกไม่กี่วินาที
if not showModal then
return
end
showModal = false
task.delay(5, function()
showModal = true
end)
MarketplaceService:PromptRobloxSubscriptionPurchase(player)
end
end)
-- ตรวจจับส่วนของอักขระที่ออกจากเทเลพอร์ต
teleporter.TouchEnded:Connect(function(otherPart)
local player = Players:GetPlayerFromCharacter(otherPart.Parent)
if player and player:GetAttribute("CharacterPartsTouching") then
player:SetAttribute("CharacterPartsTouching", player:GetAttribute("CharacterPartsTouching") - 1)
end
end)
-- มอบรางวัลเมื่อเซิร์ฟเวอร์ยืนยันการเปลี่ยนแปลงการสมัครสมาชิก
-- เชื่อมต่อการเปลี่ยนแปลง HasRobloxSubscription สำหรับแต่ละผู้เล่น
Players.PlayerAdded:Connect(function(player)
player:GetPropertyChangedSignal("HasRobloxSubscription"):Connect(function()
if player.HasRobloxSubscription
and player:GetAttribute("CharacterPartsTouching")
and player:GetAttribute("CharacterPartsTouching") > 0
then
grantRewardAndTeleport(player)
end
end)
end)PromptRobuxTransferAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
MarketplaceService:PromptRobuxTransferAsync
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
-- ตัวอย่าง: โอน Robux เมื่อผู้เล่นเรียกใช้ RemoteEvent
local transferEvent = game.ReplicatedStorage:WaitForChild("RequestTransfer")
transferEvent.OnServerEvent:Connect(function(sender, receiverUserId, amount)
-- ตรวจสอบข้อมูลการนำเข้า
if not sender or not sender:IsA("Player") then
return
end
if typeof(receiverUserId) ~= "number" or receiverUserId <= 0 then
warn("receiverUserId ไม่ถูกต้อง")
return
end
if typeof(amount) ~= "number" or amount <= 0 then
warn("จำนวนเงินไม่ถูกต้อง")
return
end
local success, result = pcall(function()
return MarketplaceService:PromptRobuxTransferAsync(sender, receiverUserId, amount)
end)
if success then
print(`เริ่มโอนด้วย TransferRequestId: {result}`)
else
warn(`การโอนล้มเหลว: {result}`)
end
end)PromptSubscriptionPurchase
RankProductsAsync
พารามิเตอร์
ส่งค่ากลับ
{RankedItem}
ตัวอย่างโค้ด
รับรายชื่อผลิตภัณฑ์ตามลำดับโดยอิงจากรหัสผลิตภัณฑ์ที่จัดเตรียมไว้
-- รับ MarketplaceService
local MarketplaceService = game:GetService("MarketplaceService")
-- สร้างอาเรย์ของผลิตภัณฑ์ที่คุณต้องการจัดอันดับ
local productIdentifiers = {
{InfoType = Enum.InfoType.GamePass, Id = 123},
{InfoType = Enum.InfoType.Product, Id = 456},
{InfoType = Enum.InfoType.Product, Id = 789}
}
-- เรียกใช้ในฟังก์ชันที่ถูกป้องกันเพื่อจัดการกับข้อผิดพลาดอย่างราบรื่น
local success, rankedProducts = pcall(function()
return MarketplaceService:RankProductsAsync(productIdentifiers)
end)
if not success then
error("ไม่สามารถจัดอันดับผลิตภัณฑ์ได้")
end
-- โหลดสินค้าที่ส่งคืนเข้าสู่ร้านค้า
for i, rankedItem in ipairs(rankedProducts) do
local productIdentifier = rankedItem.ProductIdentifier
local productInfo = rankedItem.ProductInfo
-- ...
-- ลอจิกในการเพิ่มผลิตภัณฑ์เข้าไปในร้าน
endRecommendTopProductsAsync
พารามิเตอร์
ส่งค่ากลับ
{RankedItem}
ตัวอย่างโค้ด
รับรายชื่อสินค้าที่ติดอันดับสูงสุดสำหรับในร้านภายในประสบการณ์ของคุณ
-- รับบริการ MarketplaceService
local MarketplaceService = game:GetService("MarketplaceService")
-- สร้างอาร์เรย์ของประเภทผลิตภัณฑ์ที่จะรวม ในกรณีนี้ทั้งเกมพาสและผลิตภัณฑ์สำหรับนักพัฒนา
local productTypes = {Enum.InfoType.GamePass, Enum.InfoType.Product}
-- เรียกในคำสั่งที่ถูกป้องกันเพื่อจัดการกับข้อผิดพลาดอย่างราบรื่น
local success, topRankedItems = pcall(function()
return MarketplaceService:RecommendTopProductsAsync(productTypes)
end)
if not success then
error("ไม่สามารถจัดอันดับสินค้าที่ติดอันดับได้")
end
-- โหลดรายการที่ส่งกลับเข้าสู่ร้าน ตรวจสอบให้แน่ใจว่าได้กรองรายการที่ไม่มีสิทธิ์ออกจาก topRankedItems เช่น ผลิตภัณฑ์สำหรับนักพัฒนาที่ผู้ใช้ไม่สามารถซื้อได้อีกต่อไป
for i, rankedItem in ipairs(topRankedItems) do
local productIdentifier = rankedItem.ProductIdentifier
local productInfo = rankedItem.ProductInfo
-- ...
-- Logic to add products into store
endเหตุการณ์
PromptBulkPurchaseFinished
MarketplaceService.PromptBulkPurchaseFinished(
พารามิเตอร์
PromptBundlePurchaseFinished
PromptGamePassPurchaseFinished
MarketplaceService.PromptGamePassPurchaseFinished(
ตัวอย่างโค้ด
การจัดการการซื้อ Gamepass เสร็จสิ้น
local MarketplaceService = game:GetService("MarketplaceService")
local function gamepassPurchaseFinished(...)
-- แสดงรายละเอียดทั้งหมดของการแจ้งเตือน เช่น:
-- PromptGamePassPurchaseFinished PlayerName 123456 false
print("PromptGamePassPurchaseFinished", ...)
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(gamepassPurchaseFinished)PromptProductPurchaseFinished
PromptPurchaseFinished
MarketplaceService.PromptPurchaseFinished(
ตัวอย่างโค้ด
การจัดการอีเวนต์ PromptPurchaseFinished
local MarketplaceService = game:GetService("MarketplaceService")
local function onPromptPurchaseFinished(player, assetId, isPurchased)
if isPurchased then
print(player.Name, "ซื้อสินค้าด้วย AssetID:", assetId)
else
print(player.Name, "ไม่ได้ซื้อสินค้าด้วย AssetID:", assetId)
end
end
MarketplaceService.PromptPurchaseFinished:Connect(onPromptPurchaseFinished)PromptRobloxSubscriptionPurchaseFinished
MarketplaceService.PromptRobloxSubscriptionPurchaseFinished(
ตัวอย่างโค้ด
จัดการ PromptRobloxSubscriptionPurchaseFinished เหตุการณ์
local MarketplaceService = game:GetService("MarketplaceService")
local function onPromptRobloxSubscriptionPurchaseFinished(player, didTryPurchasing)
if didTryPurchasing then
-- ผู้เล่นพยายามสมัครสมาชิก; รอการเปลี่ยนแปลง HasRobloxSubscription เพื่อยืนยัน
print(player.Name, "พยายามสมัครสมาชิก Roblox Plus")
else
print(player.Name, "ปิดการแจ้งเตือนการสมัคร Roblox Plus โดยไม่ทำการซื้อ")
end
end
MarketplaceService.PromptRobloxSubscriptionPurchaseFinished:Connect(onPromptRobloxSubscriptionPurchaseFinished)Callbacks
ProcessReceipt
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
การตอบสนอง ProcessReceipt
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
-- การตั้งค่าข้อมูลในร้านสำหรับติดตามการซื้อที่ประมวลผลสำเร็จแล้ว
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
local productIdByName = {
fullHeal = 123123,
gold100 = 456456,
}
-- พจนานุกรมเพื่อค้นหาฟังก์ชันจัดการเพื่ออนุญาตการซื้อตาม ID ผลิตภัณฑ์
-- ฟังก์ชันเหล่านี้ส่งคืน true หากการซื้อได้รับการอนุญาตอย่างสำเร็จ
-- ฟังก์ชันเหล่านี้ต้องไม่เกิดการหยุดชะงักเนื่องจากจะถูกเรียกในภายหลังภายใน UpdateAsync() callback
local grantPurchaseHandlerByProductId = {
[productIdByName.fullHeal] = function(_receipt, player)
local character = player.Character
local humanoid = character and character:FindFirstChild("Humanoid")
-- ตรวจสอบให้แน่ใจว่าผู้เล่นมีมนุษย์ที่จะรักษา
if not humanoid then
return false
end
-- รักษาผู้เล่นให้มีสุขภาพเต็มที่
humanoid.Health = humanoid.MaxHealth
-- ระบุการอนุญาตที่สำเร็จ
return true
end,
[productIdByName.gold100] = function(_receipt, player)
local leaderstats = player:FindFirstChild("leaderstats")
local goldStat = leaderstats and leaderstats:FindFirstChild("Gold")
if not goldStat then
return false
end
-- เพิ่มทอง 100 ให้กับสถิติทองของผู้เล่น
goldStat.Value += 100
-- ระบุการอนุญาตที่สำเร็จ
return true
end,
}
-- ฟังก์ชันหลักที่ตอบสนอง ProcessReceipt
-- การประยุกต์ใช้นี้จัดการสถานการณ์ล้มเหลวส่วนใหญ่แต่ไม่ได้ป้องกันสถานการณ์ข้อผิดพลาดของข้อมูลข้ามเซิร์ฟเวอร์ได้ทั้งหมด
local function processReceipt(receiptInfo)
local success, result = pcall(
purchaseHistoryStore.UpdateAsync,
purchaseHistoryStore,
receiptInfo.PurchaseId,
function(isPurchased)
if isPurchased then
-- การซื้อนี้ได้รับการบันทึกเป็นอนุญาตแล้ว ดังนั้นมันจะต้องได้รับการจัดการก่อนหน้านี้
-- หลีกเลี่ยงการเรียกฟังก์ชันจัดการการอนุญาตที่นี่เพื่อป้องกันการอนุญาตการซ้ำ
-- ขณะที่ค่าที่อยู่ในข้อมูลในร้านเป็น true แล้ว ค่า true จะถูกส่งคืนอีกครั้งเพื่อให้ตัวแปรผลลัพธ์ pcall ก็เป็น true
-- สิ่งนี้จะใช้ในภายหลังเพื่อนำกลับ PurchaseGranted จากตัวประมวลผลใบเสร็จ
return true
end
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
-- หลีกเลี่ยงการอนุญาตการซื้อหากผู้เล่นไม่ได้อยู่ในเซิร์ฟเวอร์
-- เมื่อพวกเขากลับเข้ามา ตัวประมวลผลใบเสร็จนี้จะถูกเรียกอีกครั้ง
return nil
end
local grantPurchaseHandler = grantPurchaseHandlerByProductId[receiptInfo.ProductId]
if not grantPurchaseHandler then
-- หากไม่มีผู้จัดการที่กำหนดไว้สำหรับ ID ผลิตภัณฑ์นี้ การซื้อจะต้องไม่สามารถประมวลผลได้
-- สิ่งนี้จะไม่เกิดขึ้นตราบใดที่มีกำหนดผู้จัดการสำหรับทุก ID ผลิตภัณฑ์ที่ขายในประสบการณ์
warn(`ไม่มีผู้จัดการการซื้อที่กำหนดไว้สำหรับ ID ผลิตภัณฑ์ '{receiptInfo.ProductId}'`)
return nil
end
local handlerSucceeded, handlerResult = pcall(grantPurchaseHandler, receiptInfo, player)
if not handlerSucceeded then
local errorMessage = handlerResult
warn(
`ผู้จัดการการอนุญาตการซื้อเกิดข้อผิดพลาดขณะประมวลผลการซื้อจาก '{player.Name}' ของ ID ผลิตภัณฑ์ '{receiptInfo.ProductId}': {errorMessage}`
)
return nil
end
local didHandlerGrantPurchase = handlerResult == true
if not didHandlerGrantPurchase then
-- ผู้จัดการไม่ได้อนุญาตการซื้อ ดังนั้นให้บันทึกว่าไม่ได้อนุญาต
return nil
end
-- การซื้อได้รับการอนุญาตให้กับผู้เล่นแล้ว ดังนั้นให้บันทึกว่าได้รับการอนุญาต
-- สิ่งนี้จะถูกใช้ในภายหลังเพื่อนำกลับ PurchaseGranted จากตัวประมวลผลใบเสร็จ
return true
end
)
if not success then
local errorMessage = result
warn(`ล้มเหลวในการประมวลผลใบเสร็จเนื่องจากข้อผิดพลาดของข้อมูลในร้าน: {errorMessage}`)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local didGrantPurchase = result == true
return if didGrantPurchase
then Enum.ProductPurchaseDecision.PurchaseGranted
else Enum.ProductPurchaseDecision.NotProcessedYet
end
-- ตั้งค่าการตอบสนอง; สิ่งนี้สามารถทำได้เพียงครั้งเดียวโดยสคริปต์เดียวในเซิร์ฟเวอร์
MarketplaceService.ProcessReceipt = processReceipt