You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

触发RemoteEvent无响应:购买完成后GUI未正常显示问题排查

问题排查与修复方案

一、ServerScript 核心错误

1. 条件判断语法&逻辑错误

原代码的if语句存在两处致命问题:

  • 多余的then关键字,属于语法错误,直接导致代码无法正确执行
  • 逻辑判断不完整:or后直接写产品ID的写法完全错误,Lua中所有非零数字都会被判定为true,不管当前receiptInfo.ProductId是否匹配该ID。正确写法需要每个条件都明确声明receiptInfo.ProductId == 对应产品ID。

2. 错误触发所有客户端

使用PurchaseSuccess:FireAllClients()会给所有在线玩家弹出GUI,这不符合需求,正确做法是只给完成购买的目标玩家触发,即调用FireClient(player)。

二、修正后的ServerScript代码

-- Products for purchases
local Donate1ProductID = 1296480045
local Donate2ProductID = 1296484976
local Donate3ProductID = 1296484935
local Donate4ProductID = 1296484932
local Donate5ProductID = 1296483595
local Donate6ProductID = 1296484931
local Donate7ProductID = 1296484933

-- Required depencies/services
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseSuccess = ReplicatedStorage.PurchaseSuccess

-- Code
MarketplaceService.ProcessReceipt = function(receiptInfo)
    local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
    -- 先判断玩家是否在线,避免异常
    if not player then
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    local productId = receiptInfo.ProductId
    -- 修正条件判断逻辑
    if productId == Donate1ProductID 
        or productId == Donate2ProductID 
        or productId == Donate3ProductID 
        or productId == Donate4ProductID 
        or productId == Donate5ProductID 
        or productId == Donate6ProductID 
        or productId == Donate7ProductID then
        -- 仅给当前购买的玩家触发弹窗
        PurchaseSuccess:FireClient(player)
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end

    -- 处理未匹配的产品ID
    return Enum.ProductPurchaseDecision.NotProcessedYet
end

三、LocalScript 潜在优化点

  • 确认Main.PurchaseSuccess这个GUI对象确实存在于AntiPiracyCheck界面中,且初始状态设置为隐藏(如果需要弹出后显示)
  • 建议在Tween前强制设置GUI可见,避免因初始隐藏导致动画无效果

修正后的LocalScript示例:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local PurchaseSuccess = ReplicatedStorage:WaitForChild("PurchaseSuccess")
local playerGui = player:WaitForChild("PlayerGui")
local Main = playerGui:WaitForChild("AntiPiracyCheck")

PurchaseSuccess.OnClientEvent:Connect(function ()
    -- 确保GUI可见
    Main.PurchaseSuccess.Visible = true
    Main.PurchaseSuccess:TweenPosition(
        UDim2.new(0.258, 0, 0.279, 0),
        Enum.EasingDirection.InOut,
        Enum.EasingStyle.Quad,
        1,
        true
    )
end)

内容的提问来源于stack exchange,提问作者mukeenanyafiq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 23:57:20