How to equip tool into specific slot in player backpack

Hello, I’m trying to make a system where you have a costom hotbar and inventory(which I already have). I’m trying to make it so that you can equip a tool into a certain slot in the player backpack. For Ex:

It will ask if you want to equip the tool into slot 1, 2, or 3 and etc.

I know how to parent a tool into the players backpack, but not to a certain slot in the player backpack

2 Likes

The only way to do this is by refreshing the players backpack each time they change the slot, and sorting out what goes first, to last in a table or whatever you’d prefer.

What do you mean sorting out which goes first?

depending on the slots the player picks you organize a structured table with the names, and numbers sorted from like least to greatest. then you can loop through this table, and add the tools back into the players backpack 1 by 1.

Can you please give an example?

local tooltable = {} -- make a clone tool table

for i, tool in pairs(backpack) do

local toolclone = tool:Clone() -- clone the tool

table.insert(tooltable, tool) -- insert it to the new table

tool:Destroy() -- destroy it to remove it from the backpack

end

table.insert(tooltable, yourtool, numberposition) -- insert your tool where you want it

for i, clonetool in pairs(tooltable) do

clonetool.Parent = backpack -- re-add every tool

end
3 Likes

I see this is adding the tool Into the backpack, but how about a specific slot in the backpack, like slot 3 for example.

Sure, so the sorting would be done using table.sort, or whatever you’d prefer. Here’s how the inside of the table would look afterwards:

local TableOfTools = {
[1] = toolname
[2] = toolname
[3] =  toolname
-- etc
}

Now we remove the tools from the players backpack.

for i,v in pairs(TableOfTools) do
v.Parent = nil
end

Now after that, you just add them all back into the player’s backpack.

for i,v in pairs(TableOfTools) do
v.Parent = Player.Backpack
end

The line where it says table.insert(tooltable, yourtool, numberposition)

is where you choose the number position your inserting the tool too

Change ‘numberposition’ to slot 3 for that example

So if I put a 3 there then it will equip it to slot 3?

1 Like

If not, what should I put there to equip it to slot 3?

Instead of ‘numberposition’, put ‘3’

Okay thanks, I’ll try it out later

1 Like

Thank you for helping me to understand

1 Like

Ok so, I tested it out. I didnt fully know what variables to put. I put the local script inside of starterpack, and the tool inside of serverstorage

local tooltable = {} -- make a clone tool table
local backpack = game.Players.LocalPlayer.Backpack
local tool = game.ServerStorage.Yes
local yourtool = game.ServerStorage.Yes
wait(5)
for i, tool in pairs(backpack) do

	local toolclone = tool:Clone() -- clone the tool

	table.insert(tooltable, tool) -- insert it to the new table

	tool:Destroy() -- destroy it to remove it from the backpack

end

table.insert(tooltable, yourtool, 3) -- insert your tool where you want it

for i, clonetool in pairs(tooltable) do

	clonetool.Parent = backpack -- re-add every tool

end

Thats the modified script i got an error saying that the tool isnt a vilid member of ServerStorage

local UserInput = game:GetService("UserInputService")

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Backpack = Player:WaitForChild("Backpack")

local Debounce = false

local function SortBackpack()
	local Tools = {}
	for _, Tool in ipairs(Backpack:GetChildren()) do
		if Tool:IsA("Tool") then
			table.insert(Tools, Tool.Name)
		end
	end
	
	table.sort(Tools)
	
	local SortedTools = {}
	for _, ToolName in ipairs(Tools) do
		local Tool = Backpack:FindFirstChild(ToolName)
		if Tool then
			table.insert(SortedTools, Tool)
			Tool.Parent = nil
		end
	end
	
	for _, Tool in ipairs(SortedTools) do
		Tool.Parent = Backpack
	end
end

UserInput.InputBegan:Connect(function(Input, Processed)
	if Processed then
		return
	end
	
	if Debounce then
		return
	end
	
	Debounce = true
	
	if Input.KeyCode == Enum.KeyCode.F then
		SortBackpack()
	end
	
	task.wait(5)
	Debounce = false
end)

You didn’t specify how tools should be ordered but here’s a script which will order every backpack tool alphabetically.

The resorting process is bound to the “F” key, it will also work if one of the tools is equipped by the player’s character.

https://gyazo.com/d0ae0fb4f415284fa7b5bda54a2ebb57

But how does this equip the toll to a specific slot, like slot 3?

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Backpack = Player:WaitForChild("Backpack")

local Gui = script.Parent
local Button1 = Gui.Button1
local Button2 = Gui.Button2
local Button3 = Gui.Button3
local Button4 = Gui.Button4
local Button5 = Gui.Button5
local Button6 = Gui.Button6
local Button7 = Gui.Button7
local Button8 = Gui.Button8
local Button9 = Gui.Button9
local Button10 = Gui.Button10

local Buttons = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9, Button10}

local Debounce = false

for _, Button in ipairs(Buttons) do
	Button.MouseButton1Click:Connect(function()
		if Debounce then
			return
		end
		Debounce = true
		task.delay(1, function()
			Debounce = false
		end)
		
		local CharacterTool = Character:FindFirstChildOfClass("Tool")
		if not CharacterTool then
			return
		end
				
		local Tools = {}
		for _, Tool in ipairs(Backpack:GetChildren()) do
			if Tool:IsA("Tool") then
				table.insert(Tools, Tool)
			end
		end
		
		local ButtonNumber = tonumber(Button.Name:match("(%d)$"))
		if ButtonNumber then
			if #Tools < ButtonNumber then
				return
			end
			
			table.insert(Tools, ButtonNumber, CharacterTool)
			
			for _, Tool in ipairs(Tools) do
				Tool.Parent = nil
			end
			
			for _, Tool in ipairs(Tools) do
				Tool.Parent = Backpack
			end
			
			Humanoid:EquipTool(CharacterTool)
		end
	end)
end

Here’s a system I just made, it requires a ScreenGui instance with ten buttons. When one of those buttons is pressed if the player’s character currently has a tool equipped that tool is moved to the slot represented by the button’s number (so if the tool in the first slot is equipped and the tenth button is pressed the tool is moved to slot ten).

I’ve tested this and it does work, I will attach the .rbxm file so you can test it yourself.

image

Just move the tools inside the “Tools” folder into the “StarterPack” directory.

backpacksorter.rbxm (5.2 KB)

1 Like
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Backpack = Player:WaitForChild("Backpack")

local TextBox = script.Parent

local Debounce = false

TextBox.FocusLost:Connect(function(Enter, Input)
	if Debounce then
		return
	end
	Debounce = true
	task.delay(1, function()
		Debounce = false
	end)

	local CharacterTool = Character:FindFirstChildOfClass("Tool")
	if not CharacterTool then
		return
	end

	local Tools = {}
	for _, Tool in ipairs(Backpack:GetChildren()) do
		if Tool:IsA("Tool") then
			table.insert(Tools, Tool)
		end
	end

	local TextBoxNumber = tonumber(TextBox.Text)
	if TextBoxNumber then
		if #Tools < TextBoxNumber then
			return
		end

		table.insert(Tools, TextBoxNumber, CharacterTool)

		for _, Tool in ipairs(Tools) do
			Tool.Parent = nil
		end

		for _, Tool in ipairs(Tools) do
			Tool.Parent = Backpack
		end

		Humanoid:EquipTool(CharacterTool)
	end
end)

Here’s a similar implementation but with the buttons replaced with a “TextBox” instance, when a number is entered into that textbox if the player’s character currently has a tool equipped that tool is moved to the slot indicated by the inputted number. I will provide the model file for this version as well.

image

backpacksorter.rbxm (4.7 KB)

2 Likes

Okay, thanks, can you provide information on what need to be replaced with what and where I put the script(If you havnt) and also i dont know how to use those links lol