Trying to change background color when switching tools

so im making a custom toolbar and my problem is that i have no idea how to change my circle backgroundcolor back to unequipped when switching tools. i also want the tool to equip to the newly switched tool.

local inputKeys = {
	[1] = { KeyCode = Enum.KeyCode.One, Tool = nil, toolframe = nil },
	[2] = { KeyCode = Enum.KeyCode.Two, Tool = nil, toolframe = nil },
	[3] = { KeyCode = Enum.KeyCode.Three, Tool = nil, toolframe = nil },
	[4] = { KeyCode = Enum.KeyCode.Four, Tool = nil, toolframe = nil },
	[5] = { KeyCode = Enum.KeyCode.Five, Tool = nil, toolframe = nil },
	[6] = { KeyCode = Enum.KeyCode.Six, Tool = nil, toolframe = nil },
	[7] = { KeyCode = Enum.KeyCode.Seven, Tool = nil, toolframe = nil },
	[8] = { KeyCode = Enum.KeyCode.Eight, Tool = nil, toolframe = nil },
	[9] = { KeyCode = Enum.KeyCode.Nine, Tool = nil, toolframe = nil },
	[0] = { KeyCode = Enum.KeyCode.Zero, Tool = nil, toolframe = nil },
	["savedColors"] = {
		equipped = Color3.fromRGB(18, 18, 18),
		unequipped = Color3.fromRGB(213, 195, 3)
	},
}

uis.InputBegan:Connect(function(inp, gameproc)
	if gameproc then return end
	for _, dataTable in ipairs(inputKeys) do
		if dataTable.Tool == nil then return end
		if inp.KeyCode == dataTable.KeyCode then
			local toolframe = dataTable.toolframe
			local circle:Frame = toolframe.circle
			if dataTable.Tool then
				if dataTable.Tool.Parent ~= c then
					h:EquipTool(dataTable.Tool)
					circle.BackgroundColor3 = inputKeys.savedColors.equipped
				else
					h:UnequipTools(dataTable.Tool)
					circle.BackgroundColor3 = inputKeys.savedColors.unequipped
				end
			end
		end
	end
end)

you can loop through all the buttons, and skip the one you equipped, and set it to the color.

3 Likes

you absolute legend. thanks a million for your help mate. still trying to get over the confusing switch tool logic but oh well. cheers once again man!!!

solution 4 any1 else who needs a hand:

uis.InputBegan:Connect(function(inp, gameproc)
	if gameproc then return end
	for _, dataTable in ipairs(inputKeys) do
		if dataTable.Tool == nil then return end
		if inp.KeyCode == dataTable.KeyCode then
			local toolframe = dataTable.toolframe
			local circle:Frame = toolframe.circle
			local toolButton = toolframe.toolButton
			if dataTable.Tool then
				if dataTable.Tool.Parent ~= c then
					h:EquipTool(dataTable.Tool)
					circle.BackgroundColor3 = inputKeys.savedColors.equipped.circle_color
					toolButton.BackgroundTransparency = inputKeys.savedColors.equipped.transparency
					--update the tool equip color when another key is pressed (switching tools)
					for _, otherdataTable in ipairs(inputKeys) do
						local otherCircle = otherdataTable.toolframe and otherdataTable.toolframe.circle
						if otherCircle and otherdataTable.Tool ~= dataTable.Tool then --skips the currently equipped tool so the equipped color will remain untouched(dataTable.Tool)
							otherCircle.BackgroundColor3 = inputKeys.savedColors.unequipped.circle_color--change colors of all other circles to unequipped color
							otherdataTable.toolframe.toolButton.BackgroundTransparency = inputKeys.savedColors.unequipped.transparency
						end
					end
				else
					h:UnequipTools(dataTable.Tool)
					circle.BackgroundColor3 = inputKeys.savedColors.unequipped.circle_color
					toolButton.BackgroundTransparency = inputKeys.savedColors.unequipped.transparency
				end
			end
		end
	end
end)

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