Custom inventory won't equip tool

Localscript

local plr = game.Players.LocalPlayer
local backpack = plr.Backpack
local hotbar = script.Parent
local done = false
	
for i,v in pairs(backpack:GetChildren()) do

	if v:IsA("Tool") then

		hotbar[i].ImageLabel.Image = v.TextureId
		hotbar[i].Tool.Value = v.Name

	end
		
	if not backpack:FindFirstChild(hotbar[i].Tool.Value) then
			
		hotbar[i].Tool.Value = nil
		hotbar[i].ImageLabel.Image = nil
			
	end

end

backpack.ChildAdded:Connect(function()
	
	for i,v in pairs(backpack:GetChildren()) do

		if v:IsA("Tool") then

			hotbar[i].ImageLabel.Image = v.TextureId
			hotbar[i].Tool.Value = v.Name

		end

		if not backpack:FindFirstChild(hotbar[i].Tool.Value) then

			hotbar[i].Tool.Value = nil
			hotbar[i].ImageLabel.Image = nil

		end

	end
	
end)

for i,v in pairs(hotbar:GetChildren()) do
	
	if v:IsA("ImageButton") then
		
		if v.ImageLabel.Image ~= nil then
			
			v.MouseButton1Click:Connect(function()
				
				if v.Equipped.Value == false then
					
					v.Equipped.Value = true
					done = true
					
				else
					
					v.Equipped.Value = false
					done = true
					
				end

				game.ReplicatedStorage.EquipTool:FireServer(v.Tool.Value,v.Equipped)
				done = false
				
			end)
			
		end
		
	end
	
end

Serverscript


game.ReplicatedStorage.EquipTool.OnServerEvent:Connect(function(plr,toolName,equipped)
	
	if plr and toolName then
		
		if plr.Backpack:FindFirstChild(toolName) then
			
			local char = plr.Character
			local humanoid = char.Humanoid
			
			if char and humanoid then
				
				if equipped.Value == true then
					
					humanoid:EquipTool(plr.Backpack[toolName])
					
				elseif equipped.Value == false then
					
					humanoid:UnequipTools()
					
				end
				
			end
			
		end
		
	end
	
end)

It doesn’t equip the tool, yet it still sets the values inside of the UI to true and false and the tool name, etc. What am I doing wrong?

Gif: Screen capture - 6ee84d0cf159ff705a5a13ce915dae86 - Gyazo

Did you know you can use humanoid:EquipTool or UnequipTool() on the client it will be automatically replicated to the server plus it won’t have any delay

1 Like

you don’t need to let the server equip the tool, just do it on the client, you also don’t need “equipped.value”

just check if the character already holds a tool or not. If yes, then it unequips the tool, and reequip the tool selected, or if the selected tool is already found in the character, it unequips the tool.

1 Like