Custom Hotbar dosent work and need help with scripting

Hello :wave:
So i made this custom hot bar with this script and gui:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Inventory = require(ReplicatedStorage.Module:WaitForChild("InventoryModule"))

local frame = script.Parent


local slotLabels = {}

for _, child in ipairs(frame:GetChildren()) do
	if child:IsA("TextLabel") then
		table.insert(slotLabels, child)
	end
end

table.sort(slotLabels, function(a, b)
	return a.LayoutOrder < b.LayoutOrder
end)

-- Update UI slots from inventory
local function updateHotbar()
	for i, label in ipairs(slotLabels) do
		local itemName = Inventory:GetSlot(i - 1) -- adjust for 0-based inventory
		print("Slot", i - 1, "=", itemName)
		if itemName then
			label.Text = itemName
		else
			label.Text = "[Empty]"
		end
	end
end

-- Highlight the selected slot
local function highlightSlot(index)
	for i, label in ipairs(slotLabels) do
		if i == index then
			label.BackgroundColor3 = Color3.fromRGB(255, 255, 0) -- selected color
		else
			label.BackgroundColor3 = Color3.fromRGB(139, 139, 139) -- default color
		end
	end
end

-- Map keycodes to slot numbers (adjusting to index order)
local keyToSlot = {
	[Enum.KeyCode.One] = 5,
	[Enum.KeyCode.Two] = 2,
	[Enum.KeyCode.Three] = 3,
	[Enum.KeyCode.Four] = 4,
	[Enum.KeyCode.Five] = 1,
}

-- Handle key input
UIS.InputBegan:Connect(function(input, processed)
	if processed then return end

	local slotNumber = keyToSlot[input.KeyCode]
	if slotNumber then
		local selectedItem = Inventory:GetSlot(slotNumber - 1)
		if selectedItem then
			print("You selected:", selectedItem)
		end

		highlightSlot(slotNumber)
		updateHotbar()
	end
end)

-- Initial setup
updateHotbar()
highlightSlot(0)

And my current goal was the make a limiter(how many items you can carrie), remove the roblox tool system the default gui, make that the text labels dont just say empty that they say the actual tool ther holding and disable the meaning to deequip tool(i mean you can equipp , dropp it and use it but i dont wont that its placed in the backpack).
But i have 0 clue how to do all of this :thinking:
NOTE:
I just ask for some tips or show were i can start doing somthing like this PLS dont write the scripts for me i just need tips i were and waht to do. i try as much as posible on my own to write the scripts exept i cant find the problems in my script.

i hope you all can help me with this complicated problem :saluting_face:
Thank You

First of all, to disable in the inventory it is as simple as

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

Though if it is not loaded yet this can cause issues. To make a limiter, you can do a few things. You could connect .ChildAdded() to toolbox, marking that item with a tag or saving it in a table with the players name, checking collection service to see if it is past the limit, and dropping new items if it is.
Next, you can use that same .ChildAdded (and ChildRemoved) to see when tools get added, and depending on that order, you just keep adding up and setting the TextLabel.Text to the .Name of the child.
Next, you can simply do a ChildAdded/ChildRemoved check on the Player.Character, checking if the child is a tool, and using a for loop to find it that item exists, if it doesnt, use the same function you used for the backpack.
Finally, you can use UserInputService and check if the .KeyCode is backspace, and dropping the item, though there is already a system on Roblox to do that if you prefer that instead

1 Like

One thing to note is to prevent memory leaks you do have to disconnect these functions (including the one for the backpack, which refreshes every time a player dies/joins)

is there a way of preventig tghe player from picking up the item in the first place :thinking:

You would have to make your own system for that

I think i have somthing in mind for the tool thing
One problem i just stumbeld upon when i was rewriting my script a littel for the tool name, it works but:


:sweat_smile:
im almost there but i need some how to make it so that not all are getting change just the one were the tool resides

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Inventory = require(ReplicatedStorage.Module:WaitForChild("InventoryModule"))
local player = game.Players.LocalPlayer
local char = player.Character


local frame = script.Parent

--game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) 
--[[Note:I disabled it because then i cant equip or deequip tool any 
more without backpack i have to work on that]]

local slotLabels = {}

for _, child in ipairs(frame:GetChildren()) do
	if child:IsA("TextLabel") then
		table.insert(slotLabels, child)
	end
end

table.sort(slotLabels, function(a, b)
	return a.LayoutOrder < b.LayoutOrder
end)

-- Update UI slots from inventory
local function updateHotbar()
	for i, label in ipairs(slotLabels) do
		local itemName = Inventory:GetSlot(i) -- adjust for 0-based inventory

		if itemName then
			-- Find if character currently has the tool equipped
			local tool = nil
			for _, child in ipairs(char:GetChildren()) do
				if child:IsA("Tool") then
					tool = child
					break
				end
			end

			if tool then
				label.Text = tool.Name
			else
				label.Text = itemName
				
			end
		else
			label.Text = "[Empty]"
		end
	end
end

-- Highlight the selected slot
local function highlightSlot(index)
	for i, label in ipairs(slotLabels) do
		if i == index then
			label.BackgroundColor3 = Color3.fromRGB(255, 255, 0) -- selected color
		else
			label.BackgroundColor3 = Color3.fromRGB(139, 139, 139) -- default color
		end
	end
end

-- Map keycodes to slot numbers (adjusting to index order)
local keyToSlot = {
	[Enum.KeyCode.One] = 5,
	[Enum.KeyCode.Two] = 2,
	[Enum.KeyCode.Three] = 3,
	[Enum.KeyCode.Four] = 4,
	[Enum.KeyCode.Five] = 1,
}

-- Handle key input
UIS.InputBegan:Connect(function(input, processed)
	if processed then return end

	local slotNumber = keyToSlot[input.KeyCode]
	if slotNumber then
		local selectedItem = Inventory:GetSlot(slotNumber)
		if selectedItem then
			print("You selected:", selectedItem)
		end

		highlightSlot(slotNumber)
		updateHotbar()
	end
end)

-- Initial setup
updateHotbar()
highlightSlot(0)

do have an idea how i can only change the current one?

Here:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
--local Inventory = require(ReplicatedStorage.Module:WaitForChild("InventoryModule")) -- what are the functions on here?
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local Gui = script.Parent

local Labels = {}

for _, child in ipairs(Gui:GetChildren()) do

	if child:IsA("TextLabel") then

		table.insert(Labels, child)

	end

end

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

table.sort(Labels, function(a, b)

	return a.LayoutOrder < b.LayoutOrder

end) -- basically sorts it from 1, 2, 3, etc

local function UpdateHotbar()

	local tools = {}
	local backpack = player.Backpack

	for _, tool in backpack:GetChildren() do

		if tool:IsA("Tool") then
			
			if table.find(tools, tool) then continue end

			table.insert(tools, tool)

		end

	end

	for _, tool in char:GetChildren() do

		if tool:IsA("Tool") then
			
			if table.find(tools, tool) then continue end

			table.insert(tools, tool)

		end

	end

	table.sort(tools, function(a, b)

		local aname = a

		if a:IsA("Instance") then

			aname = a.Name

		end

		local bname = b

		if b:IsA("Instance") then

			bname = b.Name

		end

		return string.len(aname) < string.len(bname)

	end)

	for i, label in Labels do

		local Tool = tools[i]
		
		label.Text = Tool and Tool.Name or "Empty"

	end

end

local function highlightSlot(index)
	for i, label in ipairs(Labels) do
		if i == index then
			label.BackgroundColor3 = Color3.fromRGB(255, 255, 0) -- selected color
		else
			label.BackgroundColor3 = Color3.fromRGB(139, 139, 139) -- default color
		end
	end
end

-- Map keycodes to slot numbers (adjusting to index order)
local keyToSlot = {
	[Enum.KeyCode.One] = 1,
	[Enum.KeyCode.Two] = 2,
	[Enum.KeyCode.Three] = 3,
	[Enum.KeyCode.Four] = 4,
	[Enum.KeyCode.Five] = 5,
}

UIS.InputBegan:Connect(function(input, processed)

	if processed then return end

	local slotNumber = keyToSlot[input.KeyCode]
	if slotNumber then

		if not char:FindFirstChildOfClass("Tool") then return highlightSlot(0) end

		highlightSlot(slotNumber)

	end

	UpdateHotbar()
	
	if input.KeyCode == Enum.KeyCode.Backspace then
		
		highlightSlot(0)
		
	end

end)

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

local backpack = player:WaitForChild("Backpack")

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

UpdateHotbar()
highlightSlot(0)

old for reference:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Inventory = require(ReplicatedStorage.Module:WaitForChild("InventoryModule"))
local player = game.Players.LocalPlayer
local char = player.Character

local frame = script.Parent

--game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) 
--[[Note:I disabled it because then i cant equip or deequip tool any 
more without backpack i have to work on that]]

local slotLabels = {}

for _, child in ipairs(frame:GetChildren()) do
	if child:IsA("TextLabel") then
		table.insert(slotLabels, child)
	end
end

table.sort(slotLabels, function(a, b)
	return a.LayoutOrder < b.LayoutOrder
end)

-- Update UI slots from inventory
local function updateHotbar()
	for i, label in ipairs(slotLabels) do
		local itemName = Inventory:GetSlot(i) -- adjust for 0-based inventory

		if itemName then
			-- Find if character currently has the tool equipped
			local tool = nil
			for _, child in ipairs(char:GetChildren()) do
				if child:IsA("Tool") then
					tool = child
					break
				end
			end

			if tool then
				label.Text = tool.Name
			else
				label.Text = itemName
				
			end
		else
			label.Text = "[Empty]"
		end
	end
end

-- Highlight the selected slot
local function highlightSlot(index)
	for i, label in ipairs(slotLabels) do
		if i == index then
			label.BackgroundColor3 = Color3.fromRGB(255, 255, 0) -- selected color
		else
			label.BackgroundColor3 = Color3.fromRGB(139, 139, 139) -- default color
		end
	end
end

-- Map keycodes to slot numbers (adjusting to index order)
local keyToSlot = {
	[Enum.KeyCode.One] = 5,
	[Enum.KeyCode.Two] = 2,
	[Enum.KeyCode.Three] = 3,
	[Enum.KeyCode.Four] = 4,
	[Enum.KeyCode.Five] = 1,
}

-- Handle key input
UIS.InputBegan:Connect(function(input, processed)
	if processed then return end

	local slotNumber = keyToSlot[input.KeyCode]
	if slotNumber then
		local selectedItem = Inventory:GetSlot(slotNumber)
		if selectedItem then
			print("You selected:", selectedItem)
		end

		highlightSlot(slotNumber)
		updateHotbar()
	end
end)

-- Initial setup
updateHotbar()
highlightSlot(0)

(i keep making mistakes and have to keep editing it :sob:)

1 Like

SO i improved your code and fixed some mistakes but it works but the order of the tools is kind random and changes and intesd of sword staying at slot 1 it moves down woards i think its becasu of a<b:


And here is the script that i made:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Inventory = require(ReplicatedStorage.Module:WaitForChild("InventoryModule")) -- what are the functions on here?
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local Gui = script.Parent

local Labels = {}

for _, child in ipairs(Gui:GetChildren()) do

	if child:IsA("TextLabel") then

		table.insert(Labels, child)

	end

end

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

table.sort(Labels, function(a, b)

	return a.LayoutOrder < b.LayoutOrder

end) -- basically sorts it from 1, 2, 3, etc

local function UpdateHotbar()

	local tools = {}
	local backpack = player.Backpack

	for _, tool in backpack:GetChildren() do

		if tool:IsA("Tool") then

			table.insert(tools, tool)

		end

	end

	for _, tool in char:GetChildren() do

		if tool:IsA("Tool") then

			table.insert(tools, tool)

		end

	end

	table.sort(tools, function(a, b)

		return string.len(a.Name) < string.len(b.Name)

	end)

	for i, label in Labels do

		local toolname = tools[i] or "[Empty]"
		label.Text = toolname and toolname.Name or "[Empty]"

	end

end

local function highlightSlot(index)
	for i, label in ipairs(Labels) do
		if i == index then
			label.BackgroundColor3 = Color3.fromRGB(255, 255, 0) -- selected color
		else
			label.BackgroundColor3 = Color3.fromRGB(139, 139, 139) -- default color
		end
	end
end

-- Map keycodes to slot numbers (adjusting to index order)
local keyToSlot = {
	[Enum.KeyCode.One] = 1,
	[Enum.KeyCode.Two] = 4,
	[Enum.KeyCode.Three] = 3,
	[Enum.KeyCode.Four] = 2,
	[Enum.KeyCode.Five] = 5
}

UIS.InputBegan:Connect(function(input, processed)

	if processed then return end

	local slotNumber = keyToSlot[input.KeyCode]
	if slotNumber then
		local selectedItem = Inventory:GetSlot(slotNumber)
		if selectedItem then
			print("You selected:", selectedItem)
		end

		highlightSlot(slotNumber)
	end

	UpdateHotbar()

end)

UpdateHotbar()
highlightSlot(0)

Sorry for respondig so long i needed to do somthing else quickly :sweat_smile:
but now im back

Hello :wave:
Its me again after some time the only problem im facing with right now is the my inventroy deck is randomly shoveling or just the names not the inventory itself:

if i fix that problem and add my cutsom tool equipe and dequip then i finished everthing i planed so far i literly made my invebtory full sytem and i thank you all (or just Profile - Zer0Wit - Developer Forum | Roblox because he was the only one who answerd) for helping me out for achiving most of my goals:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local Inventory = require(char:WaitForChild("InventoryModule")) 

local Gui = script.Parent

local Labels = {}

for _, child in ipairs(Gui:GetChildren()) do

	if child:IsA("TextLabel") then
		if child:IsA("TextLabel") and child:GetAttribute("Position") then
			table.insert(Labels, child)
		end
	end

end

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

table.sort(Labels, function(a, b)

		return (a:GetAttribute("Position") or 0) < (b:GetAttribute("Position") or 0)

end) -- basically sorts it from 1, 2, 3, etc

local function UpdateHotbar()

	local tools = {}
	local backpack = player.Backpack

	for _, tool in backpack:GetChildren() do

		if tool:IsA("Tool") then

			table.insert(tools, tool)

		end

	end

	for _, tool in char:GetChildren() do

		if tool:IsA("Tool") then

			table.insert(tools, tool)

		end

	end


	for i, label in Labels do

		local toolname = tools[i] or "[Empty]"
		label.Text = toolname and toolname.Name or "[Empty]"

	end

end

local function highlightSlot(index)
	for i, label in ipairs(Labels) do
		if i == index then
			label.BackgroundColor3 = Color3.fromRGB(255, 255, 0) -- selected color
		else
			label.BackgroundColor3 = Color3.fromRGB(139, 139, 139) -- default color
		end
	end
end

-- Map keycodes to slot numbers (adjusting to index order)
local keyToSlot = {
	[Enum.KeyCode.One] = 1,
	[Enum.KeyCode.Two] = 2,
	[Enum.KeyCode.Three] = 3,
	[Enum.KeyCode.Four] = 4,
	[Enum.KeyCode.Five] = 5
}

UIS.InputBegan:Connect(function(input, processed)

	if processed then return end

	local slotNumber = keyToSlot[input.KeyCode]
	if slotNumber then
		local selectedItem = Inventory:GetSlot(slotNumber)
		if selectedItem then
			print("You selected:", selectedItem)
		end

		highlightSlot(slotNumber)
	end

	UpdateHotbar()

end)

UpdateHotbar()
highlightSlot(0)

but i after some time still didnt figured it out waht causes this :thinking:
i hope you can help me
Thank you :saluting_face:

You need to use table.sort on the inventory, if you don’t it comes in a random order, although I do have a workaround script i made for another system if you want that

well the tabel sort pushed my gui names downwoard example:
1=sword, 2=empty, 3=empty, 4=empty then it does
1=bread, 2=sworrd, 3=empty, 4=empty thats why i removed it but even with the tabel sort it still did this shovel the item name thing :sweat:

here I’ll give you my workaround

local conn = nil

local function handle(plr)

	local currentbar = 0
	local tools = {}

	local function Backpack(bp)

		for _, tool in ipairs(bp:GetChildren()) do

			if tool:IsA("Tool") and not tool:GetAttribute("HotbarPosition") then

				currentbar += 1
				tool:SetAttribute("HotbarPosition", currentbar)
				tools[tool] = true

			end

		end

		local conn = bp.ChildAdded:Connect(function(e)

			if tools[e] or e:GetAttribute("HotbarPosition") then return end

			tools[e] = true
			currentbar += 1

			e:SetAttribute("HotbarPosition", currentbar)

		end)

	end

	Backpack(plr:WaitForChild("Backpack"))

end

call this every time a players character gets added:

players.PlayerAdded:Connect(function(plr)

	local function Added(char)

                if conn then conn:Disconnect() conn = nil end

		handle(plr)

	end

	plr.CharacterAdded:Connect(Added)

	if plr.Character then

		Added(plr.Character)

	end

end)

Then you can modify this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local Inventory = require(char:WaitForChild("InventoryModule")) 

local Gui = script.Parent

local Labels = {}

for _, child in ipairs(Gui:GetChildren()) do

	if child:IsA("TextLabel") then
		if child:IsA("TextLabel") and child:GetAttribute("Position") then
			table.insert(Labels, child)
		end
	end

end

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

table.sort(Labels, function(a, b)

		return (a:GetAttribute("Position") or 0) < (b:GetAttribute("Position") or 0)

end) -- basically sorts it from 1, 2, 3, etc

local function UpdateHotbar()

	local tools = {}
	local backpack = player.Backpack

	for _, tool in backpack:GetChildren() do

		if tool:IsA("Tool") then

			table.insert(tools, tool)

		end

	end

	for _, tool in char:GetChildren() do

		if tool:IsA("Tool") then

			table.insert(tools, tool)

		end

	end

       for i, t in tools do

                local toolname = t or "[Empty]"
                local toolslot = toolname:GetAttribute("HotbarPosition")
                if not toolslot then continue end

                local label = Labels[toolslot] -- this is assuming your labels go: 1, 2, 3, 4, 5
                label.Text = toolname
		
	end

end

local function highlightSlot(index)
	for i, label in ipairs(Labels) do
		if i == index then
			label.BackgroundColor3 = Color3.fromRGB(255, 255, 0) -- selected color
		else
			label.BackgroundColor3 = Color3.fromRGB(139, 139, 139) -- default color
		end
	end
end

-- Map keycodes to slot numbers (adjusting to index order)
local keyToSlot = {
	[Enum.KeyCode.One] = 1,
	[Enum.KeyCode.Two] = 2,
	[Enum.KeyCode.Three] = 3,
	[Enum.KeyCode.Four] = 4,
	[Enum.KeyCode.Five] = 5
}

UIS.InputBegan:Connect(function(input, processed)

	if processed then return end

	local slotNumber = keyToSlot[input.KeyCode]
	if slotNumber then
		local selectedItem = Inventory:GetSlot(slotNumber)
		if selectedItem then
			print("You selected:", selectedItem)
		end

		highlightSlot(slotNumber)
	end

	UpdateHotbar()

end)

UpdateHotbar()
highlightSlot(0)

So i managed to fix everthing and now im finished thank you all for your help :wave:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local toolequipe = game.ReplicatedStorage.RemoteEvents.ToolEquipe
local char = player.Character or player.CharacterAdded:Wait()

local Inventory = require(char:WaitForChild("InventoryModule")) 

local Gui = script.Parent

local Labels = {}

for _, child in ipairs(Gui:GetChildren()) do

	if child:IsA("TextLabel") then
		if child:IsA("TextLabel") and child:GetAttribute("Position") then
			table.insert(Labels, child)
		end
	end

end

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

table.sort(Labels, function(a, b)

		return (a:GetAttribute("Position") or 0) < (b:GetAttribute("Position") or 0)

end) 

local function UpdateHotbar()
	-- First, gather all current tools from Backpack and Character
	local toolsInGame = {}
	for _, parent in ipairs({player.Backpack, char}) do
		for _, tool in ipairs(parent:GetChildren()) do
			if tool:IsA("Tool") then
				table.insert(toolsInGame, tool)
			end
		end
	end

	-- Clean up Inventory.Slots if tool is no longer valid
	for index, tool in pairs(Inventory.Slots) do
		if tool and not table.find(toolsInGame, tool) then
			Inventory:SetSlot(index, nil)
		end
	end

	-- Assign new tools to empty slots
	for _, tool in ipairs(toolsInGame) do
		local alreadyAssigned = false
		for _, assignedTool in pairs(Inventory.Slots) do
			if assignedTool == tool then
				alreadyAssigned = true
				break
			end
		end

		if not alreadyAssigned then
			for i = 1, Inventory.MaxSlots do
				if not Inventory:GetSlot(i) then
					Inventory:SetSlot(i, tool)
					break
				end
			end
		end
	end

	-- Update UI labels
	for i, label in ipairs(Labels) do
		local tool = Inventory:GetSlot(i)
		label.Text = tool and tool.Name or "[Empty]"
	end
end

local function highlightSlot(index)
	for i, label in ipairs(Labels) do
		if i == index then
			label.BackgroundColor3 = Color3.fromRGB(255, 255, 0) -- selected color
		else
			label.BackgroundColor3 = Color3.fromRGB(139, 139, 139) -- default color
		end
	end
end

-- Map keycodes to slot numbers (adjusting to index order)
local keyToSlot = {
	[Enum.KeyCode.One] = 1,
	[Enum.KeyCode.Two] = 2,
	[Enum.KeyCode.Three] = 3,
	[Enum.KeyCode.Four] = 4,
	[Enum.KeyCode.Five] = 5
}

UIS.InputBegan:Connect(function(input, processed)

	if processed then return end

	local slotNumber = keyToSlot[input.KeyCode]
	if slotNumber then
		local selectedItem = Inventory:GetSlot(slotNumber)
		if selectedItem then
			toolequipe:FireServer(selectedItem)
			print("You selected:", selectedItem)
		end

		highlightSlot(slotNumber)
	end

	UpdateHotbar()

end)

UpdateHotbar()
highlightSlot(0)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.