[Solved]Hotbar not working correctly

Hello I’m working on a game based on booga booga and I have a issue with my hotbar system

How do I make it so the items don’t disappear in backpack when equipped to character
Code:

local hotbar = script.Parent:GetChildren()
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local lastnum = nil
local equip = {
	[1] = false,
	[2] = false,
	[3] = false,
	[4] = false,
	[5] = false,
	[6] = false
	
}
local nums = {
	[1] = "One",
	[2] = "Two",
	[3] = "Three",
	[4] = "Four",
	[5] = "Five",
	[6] = "Six"
}

local inputservice = game:GetService("UserInputService")

inputservice.InputBegan:Connect(function(input, gameprocessed)
	if input.KeyCode == Enum.KeyCode.One then
		print("e")
		script.Parent.invsound:Play()
		if equip[lastnum] ~= nil and lastnum ~= 1 then
			equip[lastnum] = false
			print(equip[lastnum])
		end
		equip[1] = not equip[1]
		if equip[1] == true then
			game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["1"].obj.Value)
		else
			game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
		end
		lastnum = 1
		print(equip[1])
		print(equip)
	end
end)
inputservice.InputBegan:Connect(function(input, gameprocessed)
	if input.KeyCode == Enum.KeyCode.Two then
		print("e")
		script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 2 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
		equip[2] = not equip[2]
		if equip[2] == true then
			game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["2"].obj.Value)
		else
			game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
		end
	lastnum = 2
	--if equip[lastnum] == true then
		--script.Parent:FindFirstChild(lastnum).BackgroundColor3 = Color3.fromRGB(24, 225, 255)
	--else
		--script.Parent:FindFirstChild(lastnum).BackgroundColor3 = Color3.fromRGB(104,104,104)
	--end
	--print(equip[lastnum])
	print(equip[2])
		print(equip)
	end
end)
inputservice.InputBegan:Connect(function(input, gameprocessed)
	if input.KeyCode == Enum.KeyCode.Three then
		print("e")
		script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 3 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
		equip[3] = not equip[3]
		if equip[3] == true then
			game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["3"].obj.Value)
		else
			game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
		end
	lastnum = 3
	--if equip[lastnum] == true then
		--script.Parent:FindFirstChild(lastnum).BackgroundColor3 = Color3.fromRGB(24, 225, 255)
	--else
		--script.Parent:FindFirstChild(lastnum).BackgroundColor3 = Color3.fromRGB(104,104,104)
	--end
	--print(equip[lastnum])
	print(equip[3])
		print(equip)
	end
end)
inputservice.InputBegan:Connect(function(input, gameprocessed)
	if input.KeyCode == Enum.KeyCode.Four then
		print("e")
		script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 4 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
		equip[4] = not equip[4]
		if equip[4] == true then
			game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["4"].obj.Value)
		else
			game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
		end
	lastnum = 4
	--if equip[lastnum] == true then
		--script.Parent:FindFirstChild(lastnum).BackgroundColor3 = Color3.fromRGB(24, 225, 255)
	--else
		--script.Parent:FindFirstChild(lastnum).BackgroundColor3 = Color3.fromRGB(104,104,104)
	--end
	--print(equip[lastnum])
	print(equip[4])
		print(equip)
	end
end)
inputservice.InputBegan:Connect(function(input, gameprocessed)
	if input.KeyCode == Enum.KeyCode.Five then
		print("e")
		script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 5 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
		equip[5] = not equip[5]
		if equip[5] == true then
			game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["5"].obj.Value)
		else
			game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
		end
	lastnum = 5
	--if equip[lastnum] == true then
		--script.Parent:FindFirstChild(lastnum).BackgroundColor3 = Color3.fromRGB(24, 225, 255)
	--else
		--script.Parent:FindFirstChild(lastnum).BackgroundColor3 = Color3.fromRGB(104,104,104)
	--end
	--print(equip[lastnum])
	print(equip[5])
		print(equip)
	end
end)
inputservice.InputBegan:Connect(function(input, gameprocessed)
	if input.KeyCode == Enum.KeyCode.Six then
		print("e")
		script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 6 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
		equip[6] = not equip[6]
		if equip[6] == true then
			game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["6"].obj.Value)
		else
			game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
		end
	lastnum = 6
	--if equip[lastnum] == true then
		--script.Parent:FindFirstChild(lastnum).BackgroundColor3 = Color3.fromRGB(24, 225, 255)
	--else
		--script.Parent:FindFirstChild(lastnum).BackgroundColor3 = Color3.fromRGB(104,104,104)
	--end
	--print(equip[lastnum])
	print(equip[6])
	print(equip)
	end
end)
script.Parent["1"].TextButton.MouseButton1Down:Connect(function()
	print("e")
	script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 1 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
	equip[1] = not equip[1]
	if equip[1] == true then
		game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["1"].obj.Value)
	else
		game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
	end
	lastnum = 1
	print(equip[1])
	print(equip)
end)
script.Parent["2"].TextButton.MouseButton1Down:Connect(function()
	print("e")
	script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 2 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
	equip[2] = not equip[2]
	if equip[2] == true then
		game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["2"].obj.Value)
	else
		game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
	end
	lastnum = 2
	print(equip[2])
	print(equip)
end)
script.Parent["3"].TextButton.MouseButton1Down:Connect(function()
	print("e")
	script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 3 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
	equip[3] = not equip[3]
	if equip[3] == true then
		game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["3"].obj.Value)
	else
		game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
	end
	lastnum = 3
	print(equip[3])
	print(equip)
end)
script.Parent["4"].TextButton.MouseButton1Down:Connect(function()
	print("e")
	script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 4 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
	equip[4] = not equip[4]
	if equip[4] == true then
		game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["4"].obj.Value)
	else
		game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
	end
	lastnum = 4
	print(equip[4])
	print(equip)
end)
script.Parent["5"].TextButton.MouseButton1Down:Connect(function()
	print("e")
	script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 5 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
	equip[5] = not equip[5]
	if equip[5] == true then
		game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["5"].obj.Value)
	else
		game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
	end
	lastnum = 5
	print(equip[5])
	print(equip)
end)
script.Parent["6"].TextButton.MouseButton1Down:Connect(function()
	print("e")
	script.Parent.invsound:Play()
	if equip[lastnum] ~= nil and lastnum ~= 6 then
		equip[lastnum] = false
		print(equip[lastnum])
	end
	equip[6] = not equip[6]
	if equip[6] == true then
		game.Players.LocalPlayer.Character.Humanoid:EquipTool(script.Parent["6"].obj.Value)
	else
		game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
	end
	lastnum = 6
	print(equip[6])
	print(equip)
end)
function refreshgui()
	for i, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
		script.Parent:FindFirstChild(i).TextLabel.Text = v.Name
		
		script.Parent:FindFirstChild(i).obj.Value = v
	end
end

while wait() do
	refreshgui()
	for i, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
		if equip[1] == true then
			script.Parent:FindFirstChild("1").BackgroundColor3 = Color3.fromRGB(24, 225, 255)
		else
			script.Parent:FindFirstChild("1").BackgroundColor3 = Color3.fromRGB(104,104,104)
		end
		if equip[2] == true then
			script.Parent:FindFirstChild("2").BackgroundColor3 = Color3.fromRGB(24, 225, 255)
		else
			script.Parent:FindFirstChild("2").BackgroundColor3 = Color3.fromRGB(104,104,104)
		end
		if equip[3] == true then
			script.Parent:FindFirstChild("3").BackgroundColor3 = Color3.fromRGB(24, 225, 255)
		else
			script.Parent:FindFirstChild("3").BackgroundColor3 = Color3.fromRGB(104,104,104)
		end
		if equip[4] == true then
			script.Parent:FindFirstChild("4").BackgroundColor3 = Color3.fromRGB(24, 225, 255)
		else
			script.Parent:FindFirstChild("4").BackgroundColor3 = Color3.fromRGB(104,104,104)
		end
		if equip[5] == true then
			script.Parent:FindFirstChild("5").BackgroundColor3 = Color3.fromRGB(24, 225, 255)
		else
			script.Parent:FindFirstChild("5").BackgroundColor3 = Color3.fromRGB(104,104,104)
		end
		if equip[6] == true then
			script.Parent:FindFirstChild("6").BackgroundColor3 = Color3.fromRGB(24, 225, 255)
		else
			script.Parent:FindFirstChild("6").BackgroundColor3 = Color3.fromRGB(104,104,104)
		end
	end
end

Video of the issue:

Edit:Issue fixed

I don’t see the problem, but you should probably condense all of the :Connects into one :Connect with an if statement for future readability.

1 Like

The video shows the issue

When you equip a item the backpack removes the item until you unequip it

I see - I was just not looking at it very closely.

The problem might be in your refreshgui function:
when you equip an item, it goes out of the backpack, so maybe check the LocalPlayer.Character for items?

Also please replace the while wait() do with something better, like a RenderStepped connection or connect it to a tool being equipped/unequipped.

what is the issue exactly? I also don’t see an issue and I am confused what your are trying to say

When he equips a tool, the labels in the hotbar get messed up.

Ok I see. The code provided looks very messy. Im sure there is a better way to write it instead of doing 5 if statements for each item

Try making a dictionary of all the buttons and when something is equipped, check the item in that gui spot and then make the color grey instead of the 6 if statements for each loop. I think this may be the problem

Ok so what I’m trying to say any how to detect a equipped and unequipped items in a backpack since they disappear

Since the behavior of the backpack temporarily removes the tool when the player equips so it’s doing the switching in the video

Understand my problem better now?