CCCPaste

auto equip script (testing)

-- LocalScript (put in StarterPlayerScripts)

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")

-- the name of the tool we want to keep re-equipped; nil when nothing selected
local desiredToolName = nil

-- helper: find a Tool instance by name in Backpack or Character
local function findToolByName(name)
	if not name then return nil end
	local char = player.Character
	local tool = backpack:FindFirstChild(name)
	if tool and tool:IsA("Tool") then return tool end
	if char then
		local t2 = char:FindFirstChild(name)
		if t2 and t2:IsA("Tool") then return t2 end
	end
	return nil
end

-- equip a Tool instance using the humanoid (works client-side)
local function equipToolInstance(tool)
	if not tool then return end
	local char = player.Character
	if not char then return end
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	if humanoid then
		pcall(function() humanoid:EquipTool(tool) end)
	end
end

-- equip the first tool in the backpack (by order), set it as desired
local function equipFirstToolInBackpack()
	for _, obj in ipairs(backpack:GetChildren()) do
		if obj:IsA("Tool") then
			desiredToolName = obj.Name
			equipToolInstance(obj)
			return
		end
	end
	-- nothing found
	desiredToolName = nil
end

-- Called when a tool is unequipped. If it's our desired tool, re-equip it.
local function onToolUnequipped(tool)
	if not desiredToolName then return end
	if not tool or not tool:IsA("Tool") then return end
	-- match by name to handle tool instances switching between Backpack/Character
	if tool.Name == desiredToolName then
		-- small delay so Roblox finishes the unequip process
		task.wait(0.05)
		local candidate = findToolByName(desiredToolName)
		if candidate then
			equipToolInstance(candidate)
		end
	end
end

-- connect Unequipped for tools currently in backpack and for future ones
local function bindToolConnections()
	-- connect existing tools
	for _, obj in ipairs(backpack:GetChildren()) do
		if obj:IsA("Tool") then
			obj.Unequipped:Connect(function() onToolUnequipped(obj) end)
		end
	end
	-- connect tools added later
	backpack.ChildAdded:Connect(function(child)
		if child:IsA("Tool") then
			child.Unequipped:Connect(function() onToolUnequipped(child) end)
		end
	end)
	-- if tools move into the character (equipped), also connect them
	player.CharacterAdded:Connect(function(char)
		-- connect any tools that are in character
		char.ChildAdded:Connect(function(c)
			if c:IsA("Tool") then
				c.Unequipped:Connect(function() onToolUnequipped(c) end)
			end
		end)
	end)
end

-- input handler: press "1" to equip first tool and start the auto-reequip behavior
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.One then
			equipFirstToolInBackpack()
		end
	end
end)

-- set up connections
bindToolConnections()

-- also handle case when character spawns and we already flagged a desired tool:
player.CharacterAdded:Connect(function()
	-- small delay to let humanoid exist
	task.wait(0.1)
	if desiredToolName then
		local t = findToolByName(desiredToolName)
		if t then equipToolInstance(t) end
	end
end)