当我购买 DevProduct (Roblox LUA) 时,我怎样才能让事情发生?
Posted
技术标签:
【中文标题】当我购买 DevProduct (Roblox LUA) 时,我怎样才能让事情发生?【英文标题】:How can I make something happen when I buy a DevProduct (Roblox LUA) 【发布时间】:2021-12-20 08:49:53 【问题描述】:我已经完成了代码的主要部分,但我卡在了重要部分。这是我正在做的事情:
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local productID = 1218445231
local productInfo = MarketplaceService:GetProductInfo(productID, Enum.InfoType.Product)
script.Parent.MouseButton1Click:Connect(function()
local function promptPurchase()
local player = Players.LocalPlayer
MarketplaceService:PromptProductPurchase(player, productID)
purchased = true
end
end)
if purchased == true then
--stuck here (if you don't understand, the tsunami that I've made is supposed to become visible and start moving towards the map, however the part is in "Workspace". The button is in "StarterGUI". Please help.)
end
编辑:现在更新了代码,仍然不知道该怎么做。我可以获得工作空间服务吗?如果是这样,我如何编码将海啸的透明度设置为“0”并开始移动?这是我的代码:
local MarketplaceService = game:GetService("MarketplaceService")
local player = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage.PurchaseEvent
local productID = 1218445231
script.Parent.MouseButton1Click:Connect(function()
PurchaseEvent:FireServer(productID)
end)
if MarketplaceService:PlayerOwnsAsset(1218445231) then
--Make tsunami become visible and start moving
end
【问题讨论】:
问题是什么?什么不工作?请添加更多信息,以便我们为您提供帮助。 您能否提供更多代码和上下文来说明您遇到的问题是什么 【参考方案1】:在MarketplaceService 的文档中,有一条注释
回调ProcessReceipt 必须明确定义,以免交易失败。
在向玩家显示购买提示后,将调用ProcessReceipt
并显示他们选择的结果。因此,回调是在用户购买某物后如何让某事发生的方式。
这种代码的一个很好的结构是在服务器脚本中处理产品购买,并让 UI 元素使用 RemoteEvents 传达购买意图。所以做一些设置:
在 ReplicatedStorage 中创建一个 RemoteEvent,将其命名为“PurchaseEvent” 在 ServerScriptService 中创建脚本然后将您的 LocalScript 更新为如下所示:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage.PurchaseEvent
local productID = 1218445231
-- listen for when players click the button to buy the product
script.Parent.MouseButton1Click:Connect(function()
-- tell the server that we want this product
PurchaseEvent:FireServer(productID)
end)
然后将此代码添加到服务器脚本以处理购买:
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage.PurchaseEvent
-- listen for when players want to buy things :
PurchaseEvent.OnServerEvent:Connect(function(player, productID)
-- show the purchase prompt to the user
MarketplaceService:PromptProductPurchase(player, productID)
end)
-- Define what should happen if a player buys something
-- NOTE - ADD FUNCTIONS FOR EACH SPECIFIC PRODUCT
local productHandlers =
productHandlers[1218445231] = function(player)
print("TODO : spawn a tsunami")
end
-- Listen for when someone clicks on any of the buttons in the purchase prompt
MarketplaceService.ProcessReceipt = function(receiptInfo)
-- Find the player who made the purchase in the server
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
-- The player probably left the game, don't charge them yet
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- Look up handler function from 'productHandlers' table above
local handler = productHandlers[receiptInfo.ProductId]
if not handler then
error(string.format("No handler is defined for %d", receiptInfo.ProductId))
end
-- Call the handler function and catch any errors
local success, result = pcall(handler, player)
if not success or not result then
local message = table.concat(
"Error occurred while processing a product purchase",
" - ProductId: " .. tostring(receiptInfo.ProductId),
" - Player: " .. player.Name,
, "\n")
warn(message)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
return Enum.ProductPurchaseDecision.PurchaseGranted
end
如果您想检查用户过去是否购买过东西并且您不想再次向他们收费,您可以随时使用MarketplaceService:PlayerOwnsAsset 函数在显示购买提示之前进行检查。
【讨论】:
以上是关于当我购买 DevProduct (Roblox LUA) 时,我怎样才能让事情发生?的主要内容,如果未能解决你的问题,请参考以下文章