Cant get custom inventory working

local Item = script.Parent.Item
local Inv = script.Parent.Inventory

local BackPack = script.Parent.Parent.Parent:WaitForChild("Backpack")

local ItemNumber = 1

local RBXConnections = {
	
}


local function EquipTool(Number)
	print("Equipping: ", Number)
	
	if Inv:FindFirstChild(tostring(Number)) then
		local CloneTool : Tool = Inv:FindFirstChild(tostring(Number)).Refrence.Value:Clone()
		CloneTool.Parent = game.Workspace
		
		local Humanoid : Humanoid = game.Players.LocalPlayer.Character.Humanoid
		
		Humanoid:EquipTool(CloneTool)
		
		CloneTool.Unequipped:Connect(function()
			CloneTool:Destroy()
		end)
	end
end

local function MakeItem(ItemInfo)
	if ItemNumber > 9 then
		warn("Ran out of space.")
		return
	end
	
	
	local Clone = Item:Clone()
	Clone.Parent = script.Parent.Inventory
	
	Clone.Refrence.Value = ItemInfo.Object

	if game.ReplicatedStorage.WeaponDatas:FindFirstChild(Clone.Refrence.Value.Name) then
		local Require = require(game.ReplicatedStorage.WeaponDatas:FindFirstChild(Clone.Refrence.Value.Name))

		if string.find(Require.WeaponInfo.WeaponName, "rbxassetid://") then
			Clone.ItemName.Text = ""
			Clone.Click.Image = Require.WeaponInfo.WeaponName
		else
			Clone.ItemName.Text = Require.WeaponInfo.WeaponName
		end

	else
		Clone.ItemName.Text = ItemInfo.Object.Name
	end

	Clone.ItemNumber.Text = tostring(ItemNumber)
	Clone.Name = tostring(ItemNumber)
	Clone.Visible = true

	local Connection = Clone.Click.MouseButton1Click:Connect(function()
		EquipTool(tonumber(Clone.ItemNumber.Text))
	end)

	table.insert(RBXConnections, Connection)
	
	ItemNumber += 1
end

local function ClearHotbar()
	table.clear(RBXConnections)
	ItemNumber = 1
	
	for _, V in Inv:GetChildren() do
		if V:IsA("Frame") then
			V:Destroy()
		end
	end
end

local function UpdateHotbar()
	ClearHotbar()
	
	for _, V in BackPack:GetChildren() do
		MakeItem({Object = V})
	end
end


UpdateHotbar()


local UIS = game.UserInputService

UIS.InputBegan:Connect(function(Inp, GPE)
	if Inp.KeyCode == Enum.KeyCode.One then
		EquipTool(1)
	elseif Inp.KeyCode == Enum.KeyCode.Two then
		EquipTool(2)
	elseif Inp.KeyCode == Enum.KeyCode.Three then
		EquipTool(3)
	elseif Inp.KeyCode == Enum.KeyCode.Four then
		EquipTool(4)
	elseif Inp.KeyCode == Enum.KeyCode.Five then
		EquipTool(5)
	elseif Inp.KeyCode == Enum.KeyCode.Six then
		EquipTool(6)
	elseif Inp.KeyCode == Enum.KeyCode.Seven then
		EquipTool(7)
	elseif Inp.KeyCode == Enum.KeyCode.Eight then
		EquipTool(8)
	elseif Inp.KeyCode == Enum.KeyCode.Nine then
		EquipTool(9)
	end
end)


BackPack.ChildAdded:Connect(UpdateHotbar)
BackPack.ChildRemoved:Connect(UpdateHotbar)

I cant get this system working properly, it loads all the items, it detects when you click or use a number key, but it wont equip the tools. It just puts them in workspace and they fall through the map.

I know what im doing in not very good, but i have no idea how to make a custom inventory, and all the toolbox ones dont fit what i need, or fix the issues with the old system.

I just want an inventory system with tooltips, proper numbering, and a accessible backpack with “~”.

The reason your tool ends up in the Workspace (and falls through the map) is because you’re doing:

CloneTool.Parent = game.Workspace

Instead, you should parent it to the character:

CloneTool.Parent = Character

Also, make sure you’re doing this on the server, not in a LocalScript. Tools need to be parented to the character on the server to replicate properly otherwise, other players and server-side systems won’t recognize the tool.

1 Like

:skull::skull::skull::skull::skull:what is this bro, worst code ive seen in a WHILE

Real, just make a hashmap and loop through (OP please do this, it just makes it not a pain to look at)

Cleaned it up a bit. While sticking with your design.

Script
local item = script.Parent.Item
local inv = script.Parent.Inventory
local backpack = script.Parent.Parent.Parent:WaitForChild("Backpack")
local uis = game.UserInputService
local conn = {}
local itemNum = 1

function equipTool(n)
	if not inv:FindFirstChild(tostring(n)) then return end
	local ref = inv[tostring(n)].Refrence.Value
	local tool = ref:Clone()
	
	--tool.Parent = workspace
	tool.Parent = game.Players.LocalPlayer.Character
	game.Players.LocalPlayer.Character.Humanoid:EquipTool(tool)
	
	tool.Unequipped:Connect(function() tool:Destroy() end)
end

function makeItem(data)
	if itemNum > 9 then return end
	local clone = item:Clone()
	clone.Parent = inv
	clone.Refrence.Value = data.Object

	local d = game.ReplicatedStorage.WeaponDatas:FindFirstChild(clone.Refrence.Value.Name)
	if d then
		local info = require(d).WeaponInfo
		if string.find(info.WeaponName, "rbxassetid://") then
			clone.ItemName.Text = ""
			clone.Click.Image = info.WeaponName
		else clone.ItemName.Text = info.WeaponName
		end
	else clone.ItemName.Text = data.Object.Name
	end

	clone.ItemNumber.Text = tostring(itemNum)
	clone.Name = tostring(itemNum)
	clone.Visible = true

	table.insert(conn, clone.Click.MouseButton1Click:Connect(function()
		equipTool(tonumber(clone.ItemNumber.Text))
	end)) itemNum += 1
end

function clearHotbar()
	table.clear(conn) itemNum = 1
	for _, v in inv:GetChildren() do
		if v:IsA("Frame") then v:Destroy() end
	end
end

function updateHotbar()
	clearHotbar()
	for _, v in backpack:GetChildren() do
		makeItem({Object = v})
	end
end

uis.InputBegan:Connect(function(inp, gpe)
	if gpe then return end
	local k = inp.KeyCode.Value - Enum.KeyCode.One.Value + 1
	if k > 0 and k < 10 then equipTool(k) end
end)

backpack.ChildAdded:Connect(updateHotbar)
backpack.ChildRemoved:Connect(updateHotbar)
updateHotbar()

this works, but pressing number keys no longer does

this was for testing, i dont know how to optimize it and i was going to figure that out later

Updated that script and tested the keypress.

uis.InputBegan:Connect(function(inp, gpe)
	if gpe then return end
	local k = inp.KeyCode.Value - Enum.KeyCode.One.Value + 1
	if k > 0 and k < 10 then equipTool(k) end
end)