How do I fix the swapping function for my Inventory system?
I've been working on an inventory system for a survival game I've been working on, and I've been having trouble with it. I've been lately working specifically on the swapping function, where you can select an item, press a button, and then if you press 1 for example, the selected item will go in slot 1 of the hotbar. However, when I programmed the code for the swapping to actually happen, it didn't work. I believe it was due to the updateDisplay function and how it works, since the item's data was still in the hotbar slot.
Code is below (go to lines 116 and 188 ):
001 | local StarterGui = game:GetService( "StarterGui" ) |
002 | local Players = game:GetService( "Players" ) |
003 | local UIS = game:GetService( "UserInputService" ) |
005 | StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false ) |
007 | local player = Players.LocalPlayer |
008 | local backpack = player.Backpack |
009 | local char = player.Character |
011 | local GUI = script.Parent.Parent.Inventory |
012 | local hotbar = GUI.Hotbar |
013 | local inventory = GUI.Inventory |
014 | local items = inventory.Items |
016 | local tooltip = inventory.Tooltip |
017 | local itemNameT = inventory.Tooltip.ToolName |
018 | local itemDescription = inventory.Tooltip.Tooltip |
020 | local slot 1 = inventory.Items.InventorySlot 1 |
021 | local slot 2 = inventory.Items.InventorySlot 2 |
022 | local slot 3 = hotbar.Frame.HotbarSlot 1 |
023 | local slot 4 = hotbar.Frame.HotbarSlot 2 |
024 | local openButton = hotbar.OpenInventory |
025 | local equipButton = inventory.EquipHotbarButton |
027 | local unselctedColor = Color 3. fromRGB( 121 , 121 , 121 ) |
028 | local selectedColor = Color 3. fromRGB( 62 , 62 , 62 ) |
030 | local selectedSlot = nil |
038 | local hotbarSlotData = { |
043 | local function scanItems() |
048 | for i, tool in pairs (backpack:GetChildren()) do |
049 | if tool.ClassName = = "Tool" then |
050 | table.insert(items, tool) |
054 | for i, tool in pairs (char:GetChildren()) do |
055 | if tool.ClassName = = "Tool" then |
056 | table.insert(items, tool) |
065 | for i, tool in pairs (items) do |
066 | if invItems [ tool.Name ] then |
067 | invItems [ tool.Name ] .Count + = 1 |
068 | table.insert(invItems [ tool.Name ] .Items, tool) |
071 | invItems [ tool.Name ] = { |
081 | local function resetItems() |
082 | for i, button in pairs (items:GetChildren()) do |
083 | if button.ClassName = = "ImageButton" then |
085 | button.CountDisplay.Text = "" |
086 | button.BackgroundColor 3 = Color 3. fromRGB( 255 , 255 , 255 ) |
090 | for i, button in pairs (hotbar.Frame:GetChildren()) do |
091 | if button.ClassName = = "ImageButton" then |
093 | button.CountDisplay.Text = "" |
094 | button.BackgroundColor 3 = Color 3. fromRGB( 255 , 255 , 255 ) |
099 | local function updateSelected(selectedItem, toolData) |
105 | selectedSlot = toolData |
108 | itemNameT.Text = toolData.Items [ 1 ] .Name |
109 | itemDescription.Text = toolData.Items [ 1 ] .ToolTip |
116 | local function updateDisplay() |
119 | local invItems = scanItems() |
120 | local connection = nil |
121 | local equipButtonConnection = nil |
124 | for toolName, toolData in pairs (invItems) do |
125 | if invSlotData [ slot 1 ] = = nil or toolData.Items [ 1 ] .Name = = invSlotData [ slot 1 ] .Items [ 1 ] .Name then |
127 | invSlotData [ slot 1 ] = toolData |
129 | slot 1. CountDisplay.Text = toolData.Count |
130 | slot 1. Image = toolData.Items [ 1 ] .TextureId |
134 | connection = slot 1. MouseButton 1 Click:Connect( function () |
135 | updateSelected(slot 1 , toolData) |
138 | elseif invSlotData [ slot 2 ] = = nil or toolData.Items [ 1 ] .Name = = invSlotData [ slot 2 ] .Items [ 1 ] .Name then |
141 | invSlotData [ slot 2 ] = toolData |
143 | slot 2. CountDisplay.Text = toolData.Count |
144 | slot 2. Image = toolData.Items [ 1 ] .TextureId |
148 | connection = slot 2. MouseButton 1 Click:Connect( function () |
149 | updateSelected(slot 2 , toolData) |
152 | elseif hotbarSlotData [ slot 3 ] = = nil or toolData.Items [ 1 ] .Name = = hotbarSlotData [ slot 3 ] .Items [ 1 ] .Name then |
155 | hotbarSlotData [ slot 3 ] = toolData |
157 | slot 3. CountDisplay.Text = toolData.Count |
158 | slot 3. Image = toolData.Items [ 1 ] .TextureId |
162 | connection = slot 3. MouseButton 1 Click:Connect( function () |
163 | updateSelected(slot 3 , toolData) |
167 | elseif hotbarSlotData [ slot 4 ] = = nil or toolData.Items [ 1 ] .Name = = hotbarSlotData [ slot 4 ] .Items [ 1 ] .Name then |
169 | hotbarSlotData [ slot 4 ] = toolData |
171 | slot 4. CountDisplay.Text = toolData.Count |
172 | slot 4. Image = toolData.Items [ 1 ] .TextureId |
175 | connection = slot 4. MouseButton 1 Click:Connect( function () |
176 | updateSelected(slot 4 , toolData) |
188 | local equipButtonDebounce = false |
191 | [ Enum.KeyCode.One ] = slot 3 , |
192 | [ Enum.KeyCode.Two ] = slot 4 , |
195 | local function swapItemSlots() |
197 | if selectedSlot = = nil or equipButtonDebounce = = true then |
198 | equipButton.Text = "Can't Equip to Hotbar" |
200 | equipButton.Text = "Equip to Hotbar" |
205 | equipButtonDebounce = true |
207 | equipButton.Text = "Press a Number 1-2" |
209 | local input, gameProcessedEvent = UIS.InputBegan:Wait() |
211 | equipButton.Text = "Equip to Hotbar" |
215 | local chosenHotbarSlot = nil |
217 | for keycode, slot in pairs (keysToSlots) do |
218 | if input.KeyCode = = keycode then |
219 | chosenHotbarSlot = slot |
224 | if chosenHotbarSlot = = nil then |
225 | equipButton.Text = "Wrong Button!" |
229 | equipButton.Text = "Equip to Hotbar" |
231 | for slotF, itemData in pairs (hotbarSlotData) do |
232 | if itemData.Items [ 1 ] .Name = = selectedSlot.Items [ 1 ] .Name then |
233 | hotbarSlotData [ slotF ] = nil |
234 | print (hotbarSlotData) |
239 | hotbarSlotData [ chosenHotbarSlot ] = selectedSlot |
244 | print (chosenHotbarSlot) |
246 | print (hotbarSlotData) |
249 | equipButtonDebounce = false |
253 | equipButton.MouseButton 1 Click:Connect(swapItemSlots) |