Custom backpack tools swap places upon equipping them

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)

what are the key value pairs of the tools table? here you are indexing the table with a tool name (string) as the key and then using table.insert() which would create a key value pair with an integer key

is this what you mean?

local table = {
1 = tool1,
2 = tool2,
3 = tool3,
-- repeat until the number is 6
}

a number (integer) is the key, and the value is a tool

i hope i answered this right because your question kind of confused me

in this if statement

did you mean to do

elseif not table.find(tools,tool) then
	table.insert(tools, tool)
end

uh no

but changing that doesn’t fix the problem if that’s what you’re going for D:

1 Like

when you are equipping a tool, it is moving from the backpack to the character model. This is running the toolremoved function

which ends up removing the tool from the table

this childadded event is run because the tool is moved into the character

which ends up moving the equipped tool to the end of the backpack becuase the updatetools function is run with added=true, and this condition

will always be true, so the equipped tool is moved to the end of the backpack

change the toolremoved and tooladded functions to this

function ToolRemoved(tool)
    if tool.Parent~=char and tool.Parent~= backpack then
	    UpdateTools(tool, false)
    end
end

function ToolAdded(tool)
    if not table.find(tools, tool) then
	    UpdateTools(tool, true)
    end
end
1 Like

Just use table.sort to sort your tools

sorry wasn’t at my pc :bangbang:

but uh yeah this does seem to fix it, thanks!!

1 Like

table.sort? never heard, never seen, never used

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.