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



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

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

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

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

--omitted Code
local player = game.Players.LocalPlayer
--omitted code
userName = game.Players.LocalPlayer.Name

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

我假设您拥有的服务器代码用于处理开发产品事务。你应该注意的是,购买后,购买玩家的用户ID会在receiptinfo表中传递。这里有一个例子:

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

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

最新更新