我正在尝试让玩家背包中的物品出现在销售用户界面上,问题是我不知道从哪里开始。
这是显示的代码:
local player = game.Players.LocalPlayer
local pack = player.Backpack
local itemSell = script.Parent.Item1
local items = pack:GetChildren()
for i = 1, #items do
end
空白处是我被难住的地方。我怎样才能让它发挥作用?
您可以迭代
items
中的每个键或子项,然后为每个键或子项创建一个按钮。
在下面的代码中,对于
items
中的每个项目,我们将创建一个TextButton
。如果用户按下 TextButton,则打印用户想要出售商品。
local player = game.Players.LocalPlayer
local pack = player.Backpack
local itemSell = script.Parent.Item1
local items = pack:GetChildren()
-- Function to create a button given a tool
local function addItem(item: Tool): ()
local newButton = Instance.new("TextButton")
newButton.Name = item.Name
newButton.Text = item.Name
newButton.MouseButton1Down:Connect(function()
print(player.Name " wants to sell " .. item.Name) -- Username wants to sell item name
end)
newButton.Parent = workspace -- Change this to the frame you want the buttons parented to
end
-- Loop through each item and create a button for each
for _, item: Tool in pairs(items) do
addItem(item)
end
确保将
newButton
的父级替换为您希望按钮出现在其中的 Frame
而不是 workspace
,否则它们根本不会出现。
如果您对代码或某些部分的解释有任何疑问,请随时评论“做什么”。