Roblox Lua开发:购买DevProduct后如何触发海啸显示及移动逻辑
解决方案
核心逻辑说明
- DevProduct的购买验证必须在服务端执行,客户端的所有购买判断都不可靠,容易被篡改
PlayerOwnsAsset方法仅适用于通行证、模型等永久资产校验,无法用于消耗型DevProduct的购买状态判断- 海啸如果需要全服玩家可见,属性修改和移动逻辑必须在服务端执行,仅客户端修改的话只有购买者本人能看到效果
- 确实需要获取Workspace服务来访问海啸对象
完整实现步骤
1. 前期资源准备
- 在
ReplicatedStorage中创建一个RemoteEvent,命名为PurchaseEvent - 确认Workspace中的海啸对象命名为
Tsunami,初始属性设置:Transparency = 1,初始位置放在地图外的海域位置
2. 客户端代码(放在触发购买的按钮下的LocalScript)
local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local PurchaseEvent = ReplicatedStorage:WaitForChild("PurchaseEvent") local productID = 1218445231 script.Parent.MouseButton1Click:Connect(function() local localPlayer = Players.LocalPlayer PurchaseEvent:FireServer(productID) end)
3. 服务端代码(放在ServerScriptService下的Script中)
local MarketplaceService = game:GetService("MarketplaceService") local ReplicatedStorage = game:GetService("ReplicatedStorage") local Workspace = game:GetService("Workspace") local RunService = game:GetService("RunService") local PurchaseEvent = ReplicatedStorage:WaitForChild("PurchaseEvent") local productID = 1218445231 -- 获取海啸对象 local Tsunami = Workspace:WaitForChild("Tsunami") -- 海啸移动速度 可根据需求调整数值 local MOVE_SPEED = 3 -- 海啸移动目标位置 替换为你实际地图的对应坐标 local TARGET_POSITION = Vector3.new(0, 2, 0) -- 处理客户端发起的购买请求 PurchaseEvent.OnServerEvent:Connect(function(player, pid) if pid == productID then MarketplaceService:PromptProductPurchase(player, pid) end end) -- DevProduct购买成功官方回调 必须在此处验证购买状态 local function processReceipt(receiptInfo) if receiptInfo.ProductId == productID then -- 购买成功 触发海啸效果 -- 1. 设置海啸可见 Tsunami.Transparency = 0 Tsunami.CanCollide = true -- 2. 启动海啸移动逻辑 local moveConnection moveConnection = RunService.Heartbeat:Connect(function(deltaTime) local currentPos = Tsunami.Position local direction = (TARGET_POSITION - currentPos).Unit local newPos = currentPos + direction * MOVE_SPEED * deltaTime Tsunami.Position = newPos -- 到达目标位置后停止移动 if (newPos - TARGET_POSITION).Magnitude < 1 then moveConnection:Disconnect() -- 可选:到达后隐藏海啸 -- Tsunami.Transparency = 1 -- Tsunami.CanCollide = false end end) return Enum.ProductPurchaseDecision.PurchaseGranted end return Enum.ProductPurchaseDecision.NotProcessedYet end -- 绑定购买回调 MarketplaceService.ProcessReceipt = processReceipt
注意事项
- 代码中的
TARGET_POSITION需要替换为你实际地图中需要海啸到达的坐标 - 如果需要购买仅对当前购买者生效海啸效果,可将海啸修改逻辑移到客户端,收到服务端的购买成功通知后再执行
内容的提问来源于stack exchange,提问作者LuaLearner
相关产品推荐
相关产品推荐

