How do I get my toolbar script to not lose track of the tool in inventory when equipped

I would like to make it so that, even when a tool is equipped, the tool doesn’t leave the toolbar.

File for your convenience: Hud.rbxl - Google Drive

1 Like

Try something like this:

Put a script inside the tool, and paste this code in. This will make it so that a value is placed in the backpack when the item is equipped, holding the rarity of the equipped item. When the item is unequipped, the value is deleted and the item is returned to the backpack.

local tool = script.Parent


tool.Equipped:Connect(function()
	local name = tool.Parent.Name
	local player = game.Players:FindFirstChild(name)
	local clone = script.Parent.Rarity:Clone()
	clone.Parent = player.Backpack
end)

tool.Unequipped:Connect(function()
	local value = tool.Parent:FindFirstChild("Rarity")
	value:Destroy()
end)

Next, redo the code for the toolbar to this to support the new value storage system:

local rarities = {
	Color3.new(0.67451, 0.67451, 0.67451);
	Color3.new(0.262745, 0.705882, 0.101961);
	Color3.new(0.156863, 0.596078, 0.780392);
}
game["Run Service"].RenderStepped:Connect(function()
	local cntr = 1
	for _, i in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
		if i:IsA("Tool") or i:IsA("NumberValue") then
			script.Parent:FindFirstChild(_).BackgroundTransparency = 0
			if i.Name == "Rarity" then
				script.Parent:FindFirstChild(_).BackgroundColor3 = rarities[i.Value]
			else
				script.Parent:FindFirstChild(_).BackgroundColor3 = rarities[i.Rarity.Value]
			end
			cntr += 1
		end
	end
	for i = cntr, 5 do
		script.Parent:FindFirstChild(i).BackgroundTransparency = 0.9
		script.Parent:FindFirstChild(i).BackgroundColor3 = Color3.new(1,1,1)
	end
end)

This will make it so that the script can read the new values even when they aren’t inside their tool.

This whole system might require some tweaks and changes to fit whatever you are making, but it should get you started.