I have been trying to make a custom backpack/inventory Gui that allows players to have a better experience in my game, but it isn't quite working yet. Here is my code:

local player = game.Players.LocalPlayer 
local character = player.Character
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
function CreateButtons(location) 
	for key, value in pairs(location:GetChildren()) do 
		if value:IsA("Tool") then 
			local ImageButton = Instance.new("ImageButton", script.Parent) 
			ImageButton.Image = value.TextureId 
		        ImageButton.MouseButton1Click:connect(function() 
			      character.Humanoid:EquipTool(value) 
		       end)
	        end
	end
end

function BackpackRefresh() 
    for key, value in pairs(script.Parent:GetChildren()) do 
	   if value:IsA("ImageButton") then 
		value:Destroy()
	   end
    end
	CreateButtons(character) 
	CreateButtons(player.Backpack) 
end

BackpackRefresh() 
player.Backpack.ChildAdded:Connect(BackpackRefresh)
player.Backpack.ChildRemoved:Connect(BackpackRefresh) 
character.ChildAdded:Connect(BackpackRefresh)
character.ChildRemoved:Connect(BackpackRefresh) 

So, my code basically makes buttons for every tool it finds in a player's inventory/backpack, and it works. But there is only one problem: when the onclicked event is called and the character.Humanoid:EquipTool(value) script runs, the player doesn't have the tool equipped even though the tool has a handle. Is there something I am doing wrong? Please tell me!

6 comments

0

Your code is too messy. Don't over-comment the code.

0

Like you don't need to comment that -- when the button is clicked or -- variable for the character.

0

Though to try and fix your problem, you'll want to equip the tool server side. Doing it client sided makes the tool funky.

0

ok i will try

0

I made it cleaner so you could read it

0

Make sure that tool.Enabled is true. and use Connect: rather than connect: is its deprecated