I'm replacing roblox's inventory system with my custom made one and eveything is working out great except the problem is if you have a tool and you unequip the same key coordinated to that tool, you can't unequip that tool.
My system is where I have a full table list of what the player has in their backpack, and whenever the player presses a certain key, it converts that to a number that can index whatever tool inside of the player's backpack.
Here's the full script that makes up the inventory system:
-- // Services \\ --
local UserInputService = game:GetService("UserInputService")
local PlayersService = game:GetService("Players")
-- // Variables \\ --
local Player = PlayersService.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Backpack = Player.Backpack -- Actual backpack containing what the gear the player has
local BackpackChildren = Backpack:GetChildren()
local Copy = BackpackChildren
-- // Tables & Statements \\ --
local KeyToNumber = {
["One"] = 1,
["Two"] = 2,
["Three"] = 3,
["Four"] = 4,
["Five"] = 5,
["Six"] = 6,
["Seven"] = 7,
["Eight"] = 8,
["Nine"] = 9,
["Zero"] = 0
}
-- // Functions \\ --
local function GetToolIndex(Tool, BackpackItems)
for i,v in pairs(BackpackItems) do
if v == Tool then
return i
end
end
end
local function EquipTool(Index)
if not Humanoid:FindFirstChildOfClass("Tool") then -- if theres no tool equipped
local Tool = BackpackChildren[Index]
if Tool then
Humanoid:EquipTool(Tool)
return true
end
end
return false -- if theres a tool equipped
end
local function UnequipTool(Index) -- we know there's a tool equipped
Humanoid:UnequipTools()
-- Humanoid:EquipTool(BackpackChildren[Index])
end
local function InputStarted(Input, GameProcessedEvent)
local Conversion = KeyToNumber[Input.KeyCode.Name]
if Conversion then
if not EquipTool(Conversion) then
UnequipTool() -- if there is a tool equipped, we would want to unequip it
end
end
end
-- // Events \\ --
UserInputService.InputBegan:Connect(InputStarted)
If you would like a hands-on experience to what I'm saying, here's the link tot the place that showcases the system:
https://web.roblox.com/games/4718814317/Testing-Place-Teams
Use the system like how you would normally use for roblox's inventory system.
