Custom Hot-bar/Inventory Script Efficiency Review

So I have made an custom hot bar script which basically is like the default Roblox backpack gui but I’m planning to add a new inventory gui soon. The thing I’m concerned about is that if my code is efficient or not, everything works fine but I just want to know if there’s any better way to code it.

This is the main local script

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)

local UIS = game:GetService("UserInputService")
local HttpService = game:GetService("HttpService")
local InventoryClass = require(script:WaitForChild("Inventory")).new()

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local backpack = player:WaitForChild("Backpack")

local inputKeys = {
	One = 1;
	Two = 2;
	Three = 3;
	Four = 4;
	Five = 5;
	Six = 6;
	Seven = 7;
}

local storedInventory = InventoryClass.contents

UIS.InputBegan:Connect(function(input,gp)
	if gp then return end
	if inputKeys[input.KeyCode.Name] then
		local keyNumber = inputKeys[input.KeyCode.Name]
		if storedInventory[keyNumber] then
			local tool = storedInventory[keyNumber]
			if tool.equipped then
				tool:UnequipTool()
			else
				tool:EquipTool()
			end	
			
			for index,toolTable in pairs(storedInventory) do
				if index == keyNumber then 
					continue			
				end
				toolTable.equipped = false
				toolTable.gui.Transparency = 0
			end
			print(HttpService:JSONEncode(storedInventory))
		end
	end
end)

char.ChildAdded:Connect(function(item)
	if not item:IsA("Tool") then return end
	if not InventoryClass:CheckToolInBag(item) then
		InventoryClass:AddItem(item)
		print(HttpService:JSONEncode(storedInventory))
	end
end)

backpack.ChildAdded:Connect(function(item)
	if not InventoryClass:CheckToolInBag(item) then
		InventoryClass:AddItem(item)
		print(HttpService:JSONEncode(storedInventory))
	end
end)

backpack.ChildRemoved:Connect(function(item)
	if not InventoryClass:CheckToolInChar(item) then
		InventoryClass:RemoveItem(item)
		print(HttpService:JSONEncode(storedInventory))
	end
end)

This is the inventory class

local Inventory = {}
Inventory.__index = Inventory

local ToolClass = require(script:WaitForChild("ToolClass"))

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local guiTemplate = script:WaitForChild("Template")

function Inventory.new()
	local self = setmetatable({},Inventory)
	
	self.contents = {}
	
	return self
end

function Inventory:AddItem(item)
	local tool = ToolClass.new(item)
	table.insert(self.contents,tool)
	
	item.CanBeDropped = false
	
	local guiClone = guiTemplate:Clone()
	guiClone.Name = item.Name
	guiClone.Text = item.Name
	
	if #self.contents > 7 then
		print("Hot Bar is full")
	else
		guiClone.Parent = plr.PlayerGui.Inventory.Frame
		print("Parenting to hotbar")
	end
	
	
	tool:SetGui(guiClone)
	
end

function Inventory:RemoveItem(item)
	for index,toolTable in pairs(self.contents) do
		if toolTable.item == item then
			toolTable.gui:Destroy()
			table.remove(self.contents,index)
			break
		end
	end
	for index,toolTable in pairs(self.contents) do
		print("Checking again")
		if #self.contents <= 7 then
			if toolTable.gui.Parent ~= plr.PlayerGui.Inventory.Frame then
				print("Adding into hotbar")
				toolTable.gui.Parent = plr.PlayerGui.Inventory.Frame
			end
		end
	end
end

function Inventory:CheckToolInBag(item)
	for _,toolTable in pairs(self.contents) do
		if toolTable.item == item then
			return true
		end
	end
	return false
end

function Inventory:CheckToolInChar(item)
	for _,tool in pairs(char:GetChildren()) do
		if not tool:IsA("Tool") then continue end
		for _,toolTable in pairs(self.contents) do
			if toolTable.item == item then
				return true
			end
		end
	end
	return false
end

return Inventory

This is the tool class

local Tool = {}
Tool.__index = Tool

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

function Tool.new(tool)
	local self = setmetatable({},Tool)
	
	self.name = tool.name
	self.item = tool
	self.equipped = false
	self.gui = nil
	
	return self
end

function Tool:EquipTool()
	self.equipped = true
	self.gui.Transparency = 0.5
	char.Humanoid:EquipTool(self.item)
end

function Tool:UnequipTool()
	self.equipped = false
	self.gui.Transparency = 0
	char:FindFirstChild(self.name).Parent = player.Backpack
end

function Tool:SetGui(gui)
	self.gui = gui
end

return Tool

Please comment if you have any suggestions to make this code better! :smiley: Thank you very much :slight_smile:

Here’s the file if you want to look at it in game, Inventory.rbxl (25.3 KB)

13 Likes

Well I couldn’t find many, but here it is:
Instead of using:

return "Already In"

use:

return true

I think it will take longer to return a long string. Not much longer, more like a micro-micro-micro-optimization kind of thing.


local guiClone = guiTemplate:Clone()
guiClone.Parent = plr.PlayerGui.Inventory.Frame
guiClone.Name = item.Name
guiClone.Text = item.Name

Setting the parent should be the last thing you do from a thread I’ve read:

local guiClone = guiTemplate:Clone()
guiClone.Name = item.Name
guiClone.Text = item.Name
guiClone.Parent = plr.PlayerGui.Inventory.Frame
3 Likes

Alright I’ll be sure to put that in :slight_smile:

1 Like