Help making custom hotbar

I’m trying to make an inventory system and right now I want to make it so that whenever somebody triggers the proximity prompt in a tool, it moves to their backpack and it changes the gui on their toolbox to match the item they picked up. It works fine with 1 player but otherwise it glitches and sometimes the gui updates but sometimes it doesnt. I would try to record a video but my laptop isnt good enough sorry

Local script

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

local humanoid = character:WaitForChild('Humanoid')

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

local bp = player.Backpack

local ts = game:GetService('TweenService')
local rs = game:GetService('RunService')
local uis = game:GetService('UserInputService')
local cs = game:GetService('CollectionService')

local equipped = false
local lastSlot = 0

local main = script.Parent.Main

local slot1 = main['1']
local slot2 = main['2']
local slot3 = main['3']
local slot4 = main['4']
local slot5 = main['5']
local slot6 = main['6']
local slot7 = main['7']
local slot8 = main['8']
local slot9 = main['9']

local taggedTools = cs:GetTagged('ToolTag')



script.Parent.Server.UpdateUI.OnClientEvent:Connect(function(tool)
	for _, slot in pairs(main:GetChildren()) do
		if tool:isA('Tool') then
			if slot:IsA('ImageButton') then
				if slot.ToolName.Text == 'Empty' then
					slot.ToolName.Text = tool.Name
					if slot.Visible == false and slot.ToolName.Text ~= 'Empty' then
						slot.Visible = true
						break
					end
				end
			end
		end
	end
end)

Server script


local uis = game:GetService('UserInputService')
local cs = game:GetService('CollectionService')

local main = script.Parent.Main

local taggedTools = cs:GetTagged('ToolTag')


for _, tool in pairs(taggedTools) do -- adds proximity prompt to any tool in the game if it has the tag
	prompt = Instance.new('ProximityPrompt')
	prompt.Parent = tool:FindFirstChild('NotAHandle') or tool:WaitForChild('Handle')
	prompt.ActionText = 'Pick up'
	prompt.ObjectText = tool.Name
	prompt.KeyboardKeyCode = Enum.KeyCode.E
	prompt.HoldDuration = .25
	prompt.Triggered:Connect(function(player)
		tool.Parent = player.Backpack
		for _, child in pairs(tool:GetChildren()) do
			if child.Name == 'NotAHandle' then
				child.Name = 'Handle'
				child:WaitForChild('ProximityPrompt').Enabled = false
				script.UpdateUI:FireClient(player, tool)
				break
			end
		end
	end)
end

Ive tried moving it all to a server script, moving it all to a local script, and I have re written it 3 times but it’s always buggy

1 Like

Instead of using RemoteEvents for updating the UI in this scenario, I would recommend using .ChildAdded inside of the LocalScript.

game.Players.LocalPlayer.Backpack.ChildAdded:Connect(function(child) 
     -- Check if child is a tool    
     -- Create the UI element here
end)
2 Likes

Originally I was going to do that but I think ChildAdded also fires whenever a player switches tools

1 Like

Yes, you are correct.
This would require you to make a table which keeps track of which items the player currently has.
Whenever something is added/removed from the backpack/character of the player, check if the table contains that item and do what you have to do accordingly.

1 Like
local tools_in_backpack = {}
local backpack_full

function set_table_to_tools(currentTool)
	for _, tool in pairs(bp:GetChildren()) do
		tools_in_backpack[#tools_in_backpack + 1] = currentTool.Name
		print(tools_in_backpack)
		if #tools_in_backpack > 9 then
			backpack_full = true
			break
		end
	end
end

Is this a good start?

This function is good. I would still recommend utilizing :ChildAdded and :ChildRemoved down the line and checking if the item that was removed is no longer in the player’s character and not in the player’s backpack and then removing it from the table all together if both of those cases are true.

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