我正在制作一个库存系统,并且我一直在尝试制作它,以便我可以在热键栏和背包之间移动物品。这些脚本一开始工作得很好,但是当我执行其中一个操作时,另一个操作就不起作用了。如果您需要的话,我将发布整个脚本,但问题出在底部 2 个 for 循环。
game.StarterGui:SetCoreGuiEnabled(Enum. CoreGuiType. Backpack, false)
local replicatedStorage = game.ReplicatedStorage
local userInputService
local toolFolder = replicatedStorage.ToolFolder
local images = replicatedStorage.Images
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local playergui = player.PlayerGui
local inventory = playergui:WaitForChild("Inventory")
local hotbar = inventory.Hotbar
local inventoryFrame = inventory.Inventory
local itemFrame = inventoryFrame.ScrollingFrame
local hotbarTable = {}
local inventoryTable = {}
local insertedItems = {}
local keyConversions = {
[Enum.KeyCode.One] = 1,
[Enum.KeyCode.Two] = 2,
[Enum.KeyCode.Three] = 3,
[Enum.KeyCode.Four] = 4,
[Enum.KeyCode.Five] = 5,
[Enum.KeyCode.Six] = 6,
[Enum.KeyCode.Seven] = 7,
[Enum.KeyCode.Eight] = 8,
[Enum.KeyCode.Nine] = 9,
[Enum.KeyCode.Zero] = 10,
}
local function pickSlot()
for i, v in pairs(hotbarTable) do
if v.Item == "None" then
return i
end
end
end
local function addItem(item)
local slot = pickSlot()
if slot ~= nil then
hotbarTable[slot].Item = item
hotbarTable[slot].Count = 1
hotbarTable[slot].Image = images[item].Texture
table.insert(insertedItems, item)
elseif slot == nil then
inventoryTable[item] = {
Count = 1;
Image = images[item].Texture
}
end
end
local function fillInventoryTables()
for i = 1, 10 do
hotbarTable[i] = {
Item = "None";
Count = 0;
Image = "None"
}
end
for i, v in pairs(toolFolder:GetChildren()) do
if #insertedItems > 0 then
if table.find(insertedItems, v.Name) then
for x, c in pairs(hotbarTable) do
if c.Item == v.Name then
c.Count += 1
end
end
else
addItem(v.Name)
end
else
addItem(v.Name)
end
end
end
local function display()
for i, v in pairs(hotbarTable) do
hotbar[i].Image = v.Image
hotbar[i].Counter.Text = v.Count
end
for i, v in pairs(inventoryTable) do
local clone = inventoryFrame.ScrollingFrame.Sample:Clone()
clone.Visible = true
clone.Name = tostring(i)
clone.Parent = inventoryFrame.ScrollingFrame
clone.Image = v.Image
clone.Counter.Text = v.Count
end
end
local function Equip(key)
local tool = hotbarTable[key].Item
if player.Character:FindFirstChildOfClass("Tool") then
if player.Character:FindFirstChildOfClass("Tool").Name == tool then
player.Character:FindFirstChild("Humanoid"):UnequipTools()
player.Backpack[tool]:Destroy()
elseif player.Character:FindFirstChildOfClass("Tool").Name ~= tool then
player.Character:FindFirstChild("Humanoid"):UnequipTools()
player.Backpack:GetChildren()[1]:Destroy()
toolFolder[tool]:Clone().Parent = player.Backpack
player.Character:FindFirstChild("Humanoid"):EquipTool(player.Backpack:GetChildren()[1])
end
elseif not player.Character:FindFirstChild(tool) then
toolFolder[tool]:Clone().Parent = player.Backpack
player.Character:FindFirstChild("Humanoid"):EquipTool(player.Backpack:GetChildren()[1])
end
end
local function removeInventoryFrames()
for i, v in pairs(inventoryFrame.ScrollingFrame:GetChildren()) do
if v.ClassName == "ImageButton" and v.Name ~= "Sample" then
v:Destroy()
end
end
end
fillInventoryTables()
hotbarTable[1].Item = "None"
hotbarTable[1].Count = 0
hotbarTable[1].Image = "None"
display()
uis.InputBegan:Connect(function(key)
if key.UserInputType == Enum.UserInputType.Keyboard then
Equip(keyConversions[key.KeyCode])
end
end)
-- Moving items from backpack to hotbar
for i, v in ipairs(inventoryFrame.ScrollingFrame:GetChildren()) do
if v.ClassName == "ImageButton" then
v.MouseButton1Click:Connect(function()
print(1)
for x, c in pairs(hotbarTable) do
if c.Item == "None" then
c.Item = v
c.Count = v.Counter.Text
c.Image = v.Image
inventoryTable[v.Name] = nil
removeInventoryFrames()
display()
end
end
end)
end
end
-- Moving items from hotbar to backpack
for i, v in ipairs(hotbar:GetChildren()) do
if v.ClassName == "ImageButton" then
v.MouseButton1Click:Connect(function()
inventoryTable[v.Name] = {
Count = hotbarTable[tonumber(v.Name)].Count;
Image = hotbarTable[tonumber(v.Name)].Image
}
hotbarTable[tonumber(v.Name)].Item = "None"
hotbarTable[tonumber(v.Name)].Count = 0
hotbarTable[tonumber(v.Name)].Image = "None"
print(v.Name)
removeInventoryFrames()
display()
end)
end
print(hotbarTable)
end
当您调用以下函数时,之前的按钮将被销毁,因此在调用 display() 后没有 mousebutton1click 函数。只需在重建热栏按钮时添加回 mousebutton1click 函数即可。我希望这对你有意义。
removeInventoryFrames()