Problem Unequipping tool

I want to make a custom inventory and when you click 1 the tool is supposed to equip and when you click it again it is supposed to unequip. However, in my script it only equips. Is there any way to fix that?


UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		
		for i, v in pairs(plr.Backpack:GetChildren()) do
			 if v:IsA("Tool") and v.TextureId == script.Parent.Image then
					
					if v.Parent == plr.Backpack then
						plr.Character.Humanoid:EquipTool(v)
						
						
					else
						print("done")
					plr.Character.Humanoid:UnequipTools(v)
				
					
				end
				
					
				
					
					
			 end
		end
	end
end)
1 Like

When you press 1 you check if there is a specific tool in your backpack, which when equipped is not in your backpack anymore. Instead you should make a reference to the tool outside of the InputEnded and not use the loop, then you can check if that tool is in the backpack or not:

local tool = plr.Backpack:FindFirstChild("your-tool")

if tool.Parent == plr.Backpack then
    plr.Character.Humanoid:EquipTool(tool)
else
    plr.Character.Humanoid:UnequipTools()
end

I used the loop because I am checking if the tool has the same image as the Parent of the script… Since, it’s a custom inventory I use slots

You can still use the loop to reference the tool but you have to do it outside of the InputEnded:

local tool
for i,v in plr.Backpack:GetChildren() do
    if v:IsA("Tool") and v.TextureId == script.Parent.Image then
        tool = v
        break
    end
end

Oh, I didn’t think about that. I am going to try it.

I don’t know why it doesn’t even equip the tool now. Is there anything wrong in my script?

local tool
for i, v in plr.Backpack:GetChildren() do

	if v:IsA("Tool") and v.TextureId == script.Parent.Image then
		tool = v
		break
	end
end


UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		if tool.Parent == plr.Backpack then
			plr.Character.Humanoid:EquipTool(tool)


		else
			print("done")
			plr.Character.Humanoid:UnequipTools()

		end
				
	end
end)

Is there any errors? Did the tool get assigned properly? Try putting print(tool) after the loop to check if it exists.

No, it printed nil. I don’t know why.

Its possible that the tool isn’t loaded fast enough so the loop doesn’t catch it. To fix that you can also add this after the loop:

local notLoaded
notLoaded = plr.Backpack.ChildAdded:Connect(function(child)
    if child:IsA("Tool") and child.TextureId == script.Parent.Image then
        tool = child
        notLoaded:Disconnect()
    end
end)

We are disconnecting it after the proper tool was found to make sure it doesn’t get triggered every time you unequip the tool.

I modified the script like this and it didn’t work. How should I make it exactly?

local tool
for i, v in plr.Backpack:GetChildren() do

	if v:IsA("Tool") and v.TextureId == script.Parent.Image then
		tool = v
		break
	end
end

local notLoaded
notLoaded = plr.Backpack.ChildAdded:Connect(function(child)
	if child:IsA("Tool") and child.TextureId == script.Parent.Image then
		tool = child
		notLoaded:Disconnect()
	end
end)

print(tool)


UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		if tool.Parent == plr.Backpack then
			plr.Character.Humanoid:EquipTool(tool)


		else
			print("done")
			plr.Character.Humanoid:UnequipTools()

		end
				
	end
end)

I actually fixed it myself based on the information you gaved me.
Here is my corrected script.

local tool
plr.Backpack.ChildAdded:Connect(function()
	script.Parent.Changed:Connect(function(property)
		if property == "Image" then
			for i, v in plr.Backpack:GetChildren() do

				if v:IsA("Tool") and v.TextureId == script.Parent.Image then
					tool = v
					break
				end
			end
		end
	
	end)



end)

print(tool)


UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		if tool.Parent == plr.Backpack then
			plr.Character.Humanoid:EquipTool(tool)


		else
			print("done")
			plr.Character.Humanoid:UnequipTools()

		end
				
	end
end)

You should not be doing it like that as you are making a new connection every time something is added to the backpack. Based on the code I assume the image is not loaded fast enough instead of the tool, so instead you can check when image changes and then search for the tool similarly to how you are already doing it:

local tool

local function changeTool() --make a function for changing the tool since we are using it on multiple places
    for i, v in plr.Backpack:GetChildren() do
		if v:IsA("Tool") and v.TextureId == script.Parent.Image then
			tool = v
			break
		end
	end
end

if script.Parent.Image ~= "" then
    changeTool() --change the tool if image is loaded
end
script.Parent:GetPropertyChangedSignal("Image"):Connect(function()
    changeTool() --change the tool when image is changed
end)

This will make sure that whenever you change the image, the tool changes too.
As far as I know tools should be loaded before scripts run so this should be good enough, but if the tool isn’t loaded still you can make some changes to the script.

I don’t know. I tested my script and the tool changes every time the image changes

I know, but I’m specifically pointing to the fact in your script you are making a new connection every time something is added to the backpack which can ruin the performance in your game, so its best to change that!
The one I made does the same thing, but not checking when something is added to the backpack (as that isn’t needed unless you are adding the tools to it later).

Oh thanks., seems like your script works and I will use that since you said your version is better for performance.

1 Like

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