Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make the tools unequip into a folder?

So basically I'm creating three folders in Backpack and StarterGear: Swords, Magic, and Potions. But I want my tools to equip and unequip in my Backpack GUI. The only problem is that the equip works kinda works, but when you unequip it, it'll just go and stay inside the Backpack instead of inside of the "Swords" folder in the Backpack. And when you equip it again, it'll come back inside of the "Swords" folder.

Equip Button:

local equipped = script.Parent.Parent.Parent.HandlerS.Equipped
local selected = script.Parent.Parent.Parent.HandlerS.Selected
local location = script.Parent.Parent.Parent.HandlerS.Location
local player = game.Players.LocalPlayer
local character = player.Character

script.Parent.MouseButton1Click:connect(function()
    if equipped.Value == nil or equipped.Value ~= selected.Value then
        character.Humanoid:UnequipTools() -- Forces the player to unequip the tool that they equipped
        if location.Value == player.Backpack:WaitForChild("Swords") then
            character.Humanoid:EquipTool(selected.Value)
            equipped.Value = selected.Value
            script.Parent.Text = "Unequip"
        end
    else
        character.Humanoid:UnequipTools()
        equipped.Value = nil
        script.Parent.Text = "Equip"
    end
end)

1 answer

✓ Accepted answer

You're using Humanoid:UnequipTools. This is not the right thing to use as the character puts all it's items in it's Backpack. Use a for loop instead. It should iterate through the backpack's tools, check if their ClassName is equal to "Tool", and then parent the tools to the folder by gathering the character's children.

If you need the correct syntax to give you the idea of what I mean, here it is. Any commented lines are what was added to replace something.

What you'd like to know when using for loops:

Lua Libraries -- table - Useful for finding the number of children before starting a loop

For Loops - Generic Concept

if equipped.Value == nil or equipped.Value ~= selected.Value then
        character.Humanoid:UnequipTools() -- Forces the player to unequip the tool that they equipped
        if location.Value == player.Backpack:WaitForChild("Swords") then
            character.Humanoid:EquipTool(selected.Value)
            equipped.Value = selected.Value
            script.Parent.Text = "Unequip"
        end
    else
	--[[
		local children = player.Character:GetChildren()
		for i = 1, table.getn(children), 1 do
			if children[i].ClassName == "Tool" then
				children[i].Parent = player.Backpack.Swords (or something)
			end
		end
	]]--

        equipped.Value = nil
        script.Parent.Text = "Equip"
    end

Remind me of any errors I made. P.S. The correct syntax is not exactly right, but you can adjust stuff to fix it up. I'm not spoon-feeding you code, either, I explained a confusion/misunderstanding.

This discussion is preserved as it was. New answers, comments, and votes are closed.