此脚本的一个问题是,它将物品添加到玩家的背包中,而不是他们的入门装备中。只要玩家出生,背包就会被清除,而StarterGear则是持久的(请参阅
Backpack docs
).
另一个问题是,您的代码似乎是在LocalScript上执行的,这通常只适用于处理仅客户端对象(请参阅
LocalScript docs
). 您应该在ServerScriptService中的常规脚本上运行它。
以下脚本将在每个玩家加入游戏时向其StarterGear添加一个名为“tool”的工具:
local replicatedStorage = game:GetService("ReplicatedStorage")
local assets = replicatedStorage:WaitForChild("Assets")
local function addItem(player, itemName)
local item = assets:FindFirstChild(itemName)
if not item then return end
item:Clone().Parent = player:WaitForChild("StarterGear")
end
game:GetService("Players").PlayerAdded:Connect(function(player) addItem(player, "Tool") end)
(当然,只需将
Tool
放入StarterPack文件夹。)