如何在Roblox中使用dev product制作持续30秒的护盾?

当有人购买我的dev product时,我希望他们能获得一个持续30秒的可见护盾。

这是我尝试过的代码:

local mpService = game:GetService("MarketplaceService")
local Debris = game:GetService("Debris")

local function giveForcefield(player, duration)
    local character = player.Character
    if character then
        local forceField = Instance.new("ForceField")
        forceField.Visible = true
        forceField.Parent = character
        if duration then
            Debris:AddItem(forceField, duration)
        end
    end
end

mpService.ProcessReceipt = function(purchaceInfo)
    local plr = game:GetService("Players"):GetPlayerByUserId(purchaceInfo.PlayerId)
    if purchaceInfo.ProductId == xxxxxxx then


        game.Players.PlayerAdded:connect(function(plr)
                repeat wait() until plr.Character
            local char = plr.Character
            giveForcefield(plr, 30)
            local forceField = Instance.new("ForceField")
            forceField.Visible = true
            forceField.Parent = char

            end)



    end
    return Enum.ProductPurchaseDecision.PurchaseGranted
end

我可以购买dev product,但是代码运行后什么也不发生。 我尝试过很多事情但我有点迷惑。 请帮帮我。

点赞
用户2860267
用户2860267

你编码创建和销毁 ForceField 的两种方式都是有效的,但正如 Piglet 建议的那样,你的代码根本没有被调用。你的代码是这样的,“一旦有人购买了这个产品,等待另一个玩家加入并生成游戏,然后将该新玩家的 forcefield 给予他。”

通常情况下,game.Players.PlayerAdded:connect(function(plr) 用作获取 Player 对象的一种方法,但你已经通过调用 GetPlayerByUserId 获取了 Player 对象,因此可以直接使用它来获取角色模型。

另外,一条无关的信息是,只有在成功赠送产品后才应将产品标记为 PurchaseGranted。通过将 PurchaseGranted 设置为默认返回状态,你可能会将未配置的产品销售给某个人,而你将没有任何回报就拿走他们的钱。

local PlayerService = game:GetService("Players")
local Debris = game:GetService("Debris")

mpService.ProcessReceipt = function(purchaceInfo)
    -- 确保玩家仍在游戏中
    local plr = PlayerService:GetPlayerByUserId(purchaceInfo.PlayerId)
    if not plr then
        warn("Player could not be found. They might have left")
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    if purchaceInfo.ProductId == xxxxxxx then
        -- 检查他们的角色是否已生成
        local char = plr.Character
        if not char then
            warn("Could not find player's character in game")
            return Enum.ProductPurchaseDecision.NotProcessedYet
        end

        -- 赠送 force field
        local forceField = Instance.new("ForceField", char)

        -- 几秒钟后销毁 force field
        local duration = 30
        Debris:AddItem(forceField, duration)

        -- 将此项目标记为授予的
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end

    -- 不知道他们买了什么
    warn("Unprocessed item receipt", purchaseInfo)
    return Enum.ProductPurchaseDecision.NotProcessedYet
end
2021-02-28 23:26:57