Marketplace Service
*เนื้อหานี้แปลโดยใช้ 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
วิธีการ
Bind Receipt Handler
Marketplace Service: Bind Receipt Handler(
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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
)MarketplaceService:BindReceiptHandler
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
-- เปลี่ยนเป็น ID ผลิตภัณฑ์นักพัฒนาที่มอบ 100 เหรียญ
local COINS_PRODUCT_ID = 123456789
local COINS_PER_PURCHASE = 100
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
-- การส่งอาร์เรย์กรองจะผูก handler นี้เฉพาะกับ ID ผลิตภัณฑ์ที่ระบุไว้
-- ลบการกรองเพื่อลงทะเบียน handler ที่จับทั้งหมดที่ทำงานสำหรับ
-- ใบเสร็จผลิตภัณฑ์นักพัฒนาที่ไม่ได้รับการอ้างสิทธิ์โดย handler ที่กรอง
MarketplaceService:BindReceiptHandler(
Enum.ReceiptType.DeveloperProduct,
function(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
-- ผู้ซื้อไม่ได้อยู่ในเซิร์ฟเวอร์นี้; เลื่อนออกไปเพื่อให้ใบเสร็จ
-- ถูกส่งมอบอีกครั้งในภายหลังหรือครั้งถัดไปที่พวกเขาเข้าร่วมเซิร์ฟเวอร์
return Enum.ReceiptDecision.NotProcessedYet
end
-- มอบผลิตภัณฑ์เพียงครั้งเดียว แม้ว่าจะมีการส่งใบเสร็จเดียวกัน
-- มากกว่าหนึ่งครั้ง โดยใช้ PurchaseId เป็นกุญแจ Callback
-- จะบันทึกเป็นจริงเมื่อผลประโยชน์ถูกมอบให้ถาวร ดังนั้น
-- UpdateAsync จะส่งกลับเป็นจริงเมื่อใบเสร็จนี้เสร็จสิ้น
local success, granted = pcall(function()
return purchaseHistoryStore:UpdateAsync(receiptInfo.PurchaseId, function(alreadyGranted)
if alreadyGranted then
-- การส่งมอบก่อนหน้านี้ได้มอบการซื้อครั้งนี้แล้ว; ไม่ต้อง
-- มอบอีกครั้ง แต่ให้ใบเสร็จได้รับการแก้ไข
return true
end
local leaderstats = player:FindFirstChild("leaderstats")
local coins = leaderstats and leaderstats:FindFirstChild("Coins")
if not coins then
-- สถิติของผู้เล่นยังไม่พร้อมใช้งาน คืนค่า nil เพื่อยกเลิก
-- การเขียนดังนั้นไม่มีอะไรจะถูกบันทึกว่าได้รับการมอบหมายและ
-- ใบเสร็จจะถูกส่งมอบอีกครั้งในภายหลัง
return nil
end
coins.Value += COINS_PER_PURCHASE
return true
end)
end)
if not success or granted ~= true then
-- เกิดข้อผิดพลาดใน data store หรือผลประโยชน์ไม่ได้รับการมอบหมาย; ปล่อย
-- ใบเสร็จให้ยังไม่ถูกแก้ไขเพื่อให้ส่งมอบอีกครั้งในภายหลัง
return Enum.ReceiptDecision.NotProcessedYet
end
return Enum.ReceiptDecision.Processed
end,
{ COINS_PRODUCT_ID }
)Get Developer Products Async
ส่งค่ากลับ
ตัวอย่างโค้ด
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(" ")
endGet Product Info
Get Product Info Async
พารามิเตอร์
| ค่าเริ่มต้น: 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 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 productIds = {123456, 987654, 555777}
local results = {}
local remaining = #productIds
local function fetch(productId)
local success, info = pcall(function()
return MarketplaceService:GetProductInfoAsync(productId, Enum.InfoType.Product)
end)
if success then
results[productId] = info
else
warn("ไม่สามารถดึงข้อมูลสำหรับผลิตภัณฑ์ได้:", productId, info)
end
remaining -= 1
end
-- การสร้างการเรียกทั้งหมดพร้อมกันทำให้เอนจินสามารถจัดกลุ่มเป็น
-- คำขอ HTTP ที่น้อยลงโดยอัตโนมัติ
for _, productId in productIds do
task.spawn(fetch, productId)
end
-- รอให้คำขอทั้งหมดเสร็จสิ้น
while remaining > 0 do
task.wait()
end
-- ผลลัพธ์ทั้งหมดพร้อมใช้งานแล้ว
for productId, info in results do
print(info.Name, "-", info.PriceInRobux, "Robux")
endGet Roblox Subscription Details Async
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบรายละเอียดการสมัครสมาชิก 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)Get Subscription Product Info Async
พารามิเตอร์
ส่งค่ากลับ
Get Users Price Levels Async
พารามิเตอร์
ส่งค่ากลับ
{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("ไม่สามารถดึงระดับราคาได้.")
endGet User Subscription Details Async
ส่งค่ากลับ
Get User Subscription Payment History Async
ส่งค่ากลับ
ตัวอย่างโค้ด
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)Get User Subscription Status Async
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบสถานะการสมัครสมาชิกของผู้ใช้
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)Open Shop
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
เปิดร้านค้าในเกม
คำอธิบาย: โค้ดตัวอย่างนี้สาธิตวิธีการเปิดร้านค้าในเกม
สำหรับผู้เล่นท้องถิ่น ร้านค้าจะแสดงรายการที่ปรับแต่งได้ของเกม
ผ่านและผลิตภัณฑ์ของนักพัฒนา.
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
MarketplaceService:OpenShop(player)Player Owns Asset
Player Owns Asset Async
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบความเป็นเจ้าของไอเทม
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)Player Owns Bundle
Player Owns Bundle Async
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบความเป็นเจ้าของแบนเดิล
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)Prompt Bulk Purchase
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ลูกค้าในการซื้อจำนวนมาก
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 ที่ถูกเรียกจากไคลเอนต์และจากนั้นเรียกใช้การซื้อจำนวนมาก
promptBulkPurchaseEvent.OnServerEvent:Connect(function(player, items)
MarketplaceService:PromptBulkPurchase(player, items, {})
end)Prompt Bundle Purchase
Prompt Cancel Subscription
Prompt Game Pass Purchase
Prompt Premium Purchase
Prompt Product Purchase
Marketplace Service: Prompt Product Purchase(
):()
พารามิเตอร์
| ค่าเริ่มต้น: 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()Prompt Purchase
Marketplace Service: Prompt Purchase(
):()
พารามิเตอร์
| ค่าเริ่มต้น: true |
| ค่าเริ่มต้น: Default |
ส่งค่ากลับ
()
ตัวอย่างโค้ด
LocalScript (Client)
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)Prompt Roblox Subscription Purchase
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การซื้อสมาชิก 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)Prompt Robux Transfer Async
ส่งค่ากลับ
ตัวอย่างโค้ด
MarketplaceService:PromptRobuxTransferAsync
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- ตัวอย่าง: โอน Robux เมื่อผู้เล่นเรียกใช้ RemoteEvent
local transferEvent = 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)Prompt Subscription Purchase
Prompt VIP Server Purchase
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การซื้อเซิร์ฟเวอร์ VIP
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local buyButton = script.Parent
buyButton.Activated:Connect(function()
local success, errorMessage = pcall(function()
MarketplaceService:PromptVIPServerPurchase(localPlayer)
end)
if not success then
warn("ไม่สามารถเปิดการซื้อเซิร์ฟเวอร์ VIP ได้:", errorMessage)
end
end)Rank Products Async
Marketplace Service: Rank Products Async(product Identifiers:{ProductIdentifier}):{RankedItem}
พารามิเตอร์
productIdentifiers:{ProductIdentifier} |
ส่งค่ากลับ
{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
print(productIdentifier, productInfo)
-- ...
-- ลอจิกในการเพิ่มผลิตภัณฑ์เข้าสู่ร้านค้า
endRecommend Top Products Async
พารามิเตอร์
ส่งค่ากลับ
{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
print(productIdentifier, productInfo)
-- ...
-- ลอจิกในการเพิ่มผลิตภัณฑ์เข้าสู่ร้านค้า
endUser Owns Game Pass Async
ส่งค่ากลับ
ตัวอย่างโค้ด
การตรวจสอบหลายเกมพาสด้วยการจัดกลุ่มที่โปร่งใส
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local PASS_IDS = {1234567, 2345678, 3456789, 4567890}
local function checkPassesForPlayer(player: Player)
local owned = {}
local remaining = #PASS_IDS
local function check(passId)
local success, ownsPass = pcall(function()
return MarketplaceService:UserOwnsGamePassAsync(player.UserId, passId)
end)
if success and ownsPass then
table.insert(owned, passId)
end
remaining -= 1
end
-- การสร้างการเรียกทั้งหมดพร้อมกันทำให้เอนจินสามารถจัดกลุ่มพวกเขาเป็น
-- คำขอ HTTP ที่น้อยลงโดยอัตโนมัติ
for _, passId in PASS_IDS do
task.spawn(check, passId)
end
while remaining > 0 do
task.wait()
end
return owned
end
Players.PlayerAdded:Connect(function(player)
local ownedPasses = checkPassesForPlayer(player)
print(player.Name, "เป็นเจ้าของ", #ownedPasses, "พาส")
end)เหตุการณ์
Prompt Bulk Purchase Finished
Marketplace Service. Prompt Bulk Purchase Finished(
พารามิเตอร์
Prompt Bundle Purchase Finished
Prompt Game Pass Purchase Finished
Marketplace Service. Prompt Game Pass Purchase Finished(
ตัวอย่างโค้ด
การจัดการการซื้อ Gamepass เสร็จสิ้น
local MarketplaceService = game:GetService("MarketplaceService")
local function gamepassPurchaseFinished(...)
-- พิมพ์รายละเอียดทั้งหมดของการแจ้งเตือน เช่น:
-- PromptGamePassPurchaseFinished PlayerName 123456 false
print("PromptGamePassPurchaseFinished", ...)
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(gamepassPurchaseFinished)Prompt Product Purchase Finished
Prompt Purchase Finished
Marketplace Service. Prompt Purchase Finished(
ตัวอย่างโค้ด
การจัดการเหตุการณ์ 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)Prompt Roblox Subscription Purchase Finished
Marketplace Service. Prompt Roblox Subscription Purchase Finished(
ตัวอย่างโค้ด
จัดการ 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
Process Receipt
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
การประมวลผลการรับ Callback
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 หากการซื้อได้รับการมอบให้สำเร็จ
-- ฟังก์ชันเหล่านี้ต้องไม่หยุดชะงักเนื่องจากจะถูกเรียกในภายหลังภายใน callback UpdateAsync()
local grantPurchaseHandlerByProductId = {
[productIdByName.fullHeal] = function(_receipt, player)
local character = player.Character
local humanoid = character and character:FindFirstChild("Humanoid")
-- ตรวจสอบให้แน่ใจว่าผู้เล่นมี 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,
}
-- ฟังก์ชัน callback ProcessReceipt หลัก
-- การทำงานนี้จัดการกับสถานการณ์ล้มเหลวส่วนใหญ่ แต่ไม่สามารถบรรเทาสถานการณ์ล้มเหลวของข้อมูลข้ามเซิร์ฟเวอร์ได้อย่างสมบูรณ์
local function processReceipt(receiptInfo)
local success, result = pcall(
purchaseHistoryStore.UpdateAsync,
purchaseHistoryStore,
receiptInfo.PurchaseId,
function(isPurchased)
if isPurchased then
-- การซื้อนี้ได้รับการบันทึกว่าได้รับการมอบให้แล้ว ดังนั้นมันต้องได้รับการจัดการมาก่อน
-- หลีกเลี่ยงการเรียกฟังก์ชันจัดการการมอบที่นี่เพื่อป้องกันการมอบการซื้อซ้ำ
-- ขณะที่ค่าภายในข้อมูลจัดเก็บเป็นจริงอยู่แล้ว จะคืนค่าจริงอีกครั้งเพื่อให้ตัวแปรผลลัพธ์ของ pcall ก็เป็นจริง
-- สิ่งนี้จะถูกใช้ในภายหลังเพื่อคืนค่า 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
-- ตั้งค่าฟังก์ชัน callback; สิ่งนี้สามารถทำได้เพียงครั้งเดียวโดยสคริปต์หนึ่งในเซิร์ฟเวอร์
MarketplaceService.ProcessReceipt = processReceipt