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.
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 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.
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 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’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!
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