I came across this shenanigan where my tools swap places upon equipping them, it’s just really hard to explain a video is submitted with this post
i think this is by far the worst thing i’ve ever had to code, this was not fun at all
anyway, visual presentation of what happens (compressed because devforum.roblox.com doesn’t like videos above 10MB):
Code (localscript inside the backpack screenGUI):
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local uInputService = game:GetService("UserInputService")
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local backpack = plr.Backpack
local plrGui = plr.PlayerGui
local backpackUI = plrGui:WaitForChild("BackpackUI")
local backpackFrame = backpackUI:WaitForChild("BackpackFrame")
local slots = {
backpackFrame:WaitForChild("Melee_Slot"),
backpackFrame:WaitForChild("Slot_2"),
backpackFrame:WaitForChild("Slot_3"),
backpackFrame:WaitForChild("Slot_4"),
backpackFrame:WaitForChild("Slot_5"),
backpackFrame:WaitForChild("Slot_6")
}
local tools = {}
function RefreshSlots()
for i, slot in ipairs(slots) do
if tools[i] then
slot.ItemName.Text = tools[i].Name
else
slot.ItemName.Text = ""
end
end
end
function UpdateTools(tool, added) -- if i had to guess it's something to do with this function
if not tool then return end
if not added then
for i, t in ipairs(tools) do
if t == tool then
table.remove(tools, i)
break
end
end
elseif not tools[tool.Name] then
table.insert(tools, tool)
end
RefreshSlots()
end
function ToolRemoved(tool)
UpdateTools(tool, false)
end
function ToolAdded(tool)
UpdateTools(tool, true)
end
backpack.ChildAdded:Connect(ToolAdded)
backpack.ChildRemoved:Connect(ToolRemoved)
for _, tool in ipairs(backpack:GetChildren()) do
ToolAdded(tool)
end
char.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
ToolAdded(child)
end
end)
char.ChildRemoved:Connect(function(child)
if child:IsA("Tool") then
ToolRemoved(child)
end
end)
function ChangeColor()
for _,slot in backpackFrame:GetChildren() do
slot.ImageColor3 = Color3.fromRGB(255,255,255)
end
end
function OnKeyPressed(input)
local key = input.KeyCode
local keyPressed = nil
-- I apologize.
if key == Enum.KeyCode.One then
ChangeColor()
keyPressed = 1
backpackFrame.Melee_Slot.ImageColor3 = Color3.fromRGB(255,0,0)
elseif key == Enum.KeyCode.Two then
ChangeColor()
keyPressed = 2
backpackFrame.Slot_2.ImageColor3 = Color3.fromRGB(255,0,0)
elseif key == Enum.KeyCode.Three then
ChangeColor()
keyPressed = 3
backpackFrame.Slot_3.ImageColor3 = Color3.fromRGB(255,0,0)
elseif key == Enum.KeyCode.Four then
ChangeColor()
keyPressed = 4
backpackFrame.Slot_4.ImageColor3 = Color3.fromRGB(255,0,0)
elseif key == Enum.KeyCode.Five then
ChangeColor()
keyPressed = 5
backpackFrame.Slot_5.ImageColor3 = Color3.fromRGB(255,0,0)
elseif key == Enum.KeyCode.Six then
ChangeColor()
keyPressed = 6
backpackFrame.Slot_6.ImageColor3 = Color3.fromRGB(255,0,0)
end
if keyPressed and tools[keyPressed] then
humanoid:EquipTool(tools[keyPressed])
elseif keyPressed and not tools[keyPressed] then
humanoid:UnequipTools()
end
end
uInputService.InputBegan:Connect(OnKeyPressed)