如何在层次结构中使用变量而不是实际部分

How to Use Variable Instead of Actual Part In Hierarchy

我不太确定如何问这个问题,但我想在声明 child 在层次结构中的位置时使用变量。这是 ServerScriptService 中的代码:

--omitted code
    game.Players.userName.leaderstats.Robux.Value += receiptInfo.CurrencySpent

-- Omitted code

userName 是全局变量,而不是层次结构中的 child。我想用它来声明 child 我在找什么。

在 StarterPlayer.StarterPlayerScripts 中,我有一个包含全局变量的本地脚本:

--omitted Code

local player = game.Players.LocalPlayer

--omitted code

userName = game.Players.LocalPlayer.Name

全局变量仅在该脚本环境中定义。 _G table 可用于存储变量并在所有本地脚本中共享它们。与模块脚本相同。

我假设您拥有的服务器代码用于处理开发产品交易。需要注意的是,购买时,receiptinfo table 中传递了购买玩家的 userid。 这是一个例子:

local function processReceipt(receiptInfo)

    -- The line below gets the player instance by userid (as the name suggests)
    local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
    if player then
        -- What you want to when transaction was successfull
        return Enum.ProductPurchaseDecision.PurchaseGranted
    else
        game:GetService("ReplicatedStorage").PurchaseStatus:FireClient(player, false, nil, nil)
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end
end

game:GetService("MarketplaceService").ProcessReceipt = processReceipt

如果您需要任何进一步的帮助,请告诉我。