Help with custom backpack gui

I am making a custom UI which should work like the normal toolbar. I used UserInputService to check if the pressed key is the same as a key in a table. I then equip the same tool from the user’s backpack in that designated slot, like so:

local keybinds = {
	[1] = Enum.KeyCode.One,
	[2] = Enum.KeyCode.Two,
	[3] = Enum.KeyCode.Three,
	[4] = Enum.KeyCode.Four,
	[5] = Enum.KeyCode.Five,
	[6] = Enum.KeyCode.Six,
	[7] = Enum.KeyCode.Seven,
	[8] = Enum.KeyCode.Eight,
	[9] = Enum.KeyCode.Nine,
	[0] = Enum.KeyCode.Zero,

}

userinputservice.InputBegan:Connect(function(input,gameProcessed)
	if gameProcessed then return end
	if table.find(keybinds, input.KeyCode) then
		local slotNumber = table.find(keybinds, input.KeyCode)
		humanoid:EquipTool(player.Backpack:GetChildren()[slotNumber])
	end
end)

The issue with this is that once the tool is equipped, another tool will take its place because the equipped tool is now in the player’s character. Resulting in strange behaviour. In the video, I am only pressing the 1 key.

How can i prevent this behaviour?

2 Likes

How about adding humanoid:UnequipTools() before EquipTool method?

i am still only pressing the 1 key in this video.

So you are only pressing number one key on keyboard and picks up first tool to fourth tool one by one like looping?

yes. here is the code sample if you are interested. it is in the same position in the script as where the equipping happens.

if not char:FindFirstChildWhichIsA("Tool") then
	humanoid:EquipTool(player.Backpack:GetChildren()[slotNumber])
else
	humanoid:UnequipTools()
end

Because when you equip the tool. The slot1 is now whatever is inside your backpack. Let’s say you equip a Rocket Launcher from slot1 and when you equip it slot1 is now replaced with the slingshot and when you try to “unequip” slot1 it unequips the tool but equips the slot1 too.

I already know that is the issue. Do you know how I can unequip tools back to the slot they were before I had equipped them?

I don’t know well at roblox studio but how about destroying tool that in character that you don’t want to equip? cause there’s still number one tool is in player’s backpack.

No, the visuals displayed are seperate from what is actually happening. Every time a tool is equipped it is parented to the player’s character.

Hey, if you don’t mind. Can send me the whole inventory system with scripts? I’ll try my best to re-script it and fix the issues.

I’m not too bothered to explain how to fix it since I’m not good at explaining things and might make some errors.

The issue here is trying to preserve the backpack slot when equipping, correct? If so, why don’t you clone the tool into the character rather than re-parenting it, and just keep it in the backpack the whole time?

I tried that and it resulted in buggy behaviour. Some tools rely on the character to function and the scripts do not work otherwise.

I haven’t experimented with custom hotbars/backpacks too much, but off the top of my head I would have each toolbar slot assigned with the tool in the table, so that when they keybind is pressed, it equips the tool that is associated with slot 1 in the table, rather than the Nth child in the backpack. Then everytime something is added/removed to the backpack, reset its table slot

I’m not sure if that really makes sense, so ask questions if it sounds confusing, and it also may not be correct as this is just off the top of my head

i couldn’t upload it to the devforum but here it is

How could I do that though? if you’re talking about the keybinds, the table only allows two values for each object in the table.

I’d assume you create another table that also has numbers per slot which would be like

table = { [1] = nil; [2] = nil} so on so forth, then every time a tool is added you loop until you find a number that is nil up to the max slots of course, then set that to the tool. If all the slots are taken and the loop finds nothing, you just return from then

i think i understand what you’re talking about. Although this would technically solve my problem, I’m looking for a way to have each item stay in the same slot, even when equipped or unequipped. I will keep your answer in mind though!

classic inventory fixed.rbxm (11.7 KB)

1 Like

Idk if it’s been resolved by the post above, but if not, here’s my take:

You can make each item stay in it’s slot by only getting the children from the backpack once,

local Items = player.Backpack:GetChildren()

and then on input you can do

humanoid:EquipTool(Items[SlotNumber])

Why this works? We create the table ONCE and it does not change, so all the items reserve their original slots regardless of their parent

I would also suggest unequipping all tools before equipping new ones and tracking the tools being destroyed or parented to nil, and then updating the table

DUDE THI SCODE IS AMAZING YOU’RE.A GENIUS :pray: :pray: :pray: :pray: :pray: :pray: :pray: