我对 Roblox 工作室相当陌生,正在制作 PC 构建游戏。我目前正在尝试做到这一点,以便当玩家踏上“BuyBlock”时,他们会为该部分添加一个属性。使用此代码:
local list = game.Workspace.Store.GPU.BuyBlocks:GetDescendants()
while true do
for index=1, #list do
if list[index] then
list[index].Touched:Connect(function()
Touched(list[index])
end)
end
end
wait(1)
end
本质上,代码会迭代每个“BuyBlock”,直到触摸其中一个,然后调用一个函数来更改该部分的玩家属性。这部分在工作区中作为普通脚本工作,但是我意识到它需要一个 LocalScript 来更改玩家属性,所以我这样做了,添加了其余的代码并移至 StarterPlayerScripts:
local player = game.Players.LocalPlayer
local function Touched(part) -- called whenever one of the parts are touched
player:SetAttribute("GPU", part)
end
我假设 LocalScripts 无法搜索工作区,但是我无法想到如何将这两段代码拆分为本地脚本和非本地脚本。任何建议或解释将不胜感激。
如果您想使用单独的函数,请确保在参数中使用它之前使用 WaitForChild 来引用它。 可能不是解决这个问题的最佳方法,但它确实有效。
greenPart = game.Workspace:WaitForChild("绿色部分")
本地函数触动(部分) 玩家:SetAttribute("GPU", 部分) 结束
感动(绿色部分)
你可以这样做:
local part = workspace.Part or workspace:WaitForChild("Part")
local function touched(gpu)
local gpuValue = game.Players.LocalPlayer:FindFirstChild("GPUValue")
if not gpuValue then gpuValue = Instance.new("ObjectValue", game.Players.LocalPlayer) gpuValue.Name = "GPUValue" end
gpuValue.Value = gpu
end
part.Touched:Connect(touched)