if Mode=="\080\108\117\103\105\110"then UIContainer=CoreGui PluginButton=(Plugin:CreateToolbar("\066\117\105\108\100\105\110\103 \084\111\111\108\115 \098\121 \070\051\088")):CreateButton("\066\117\105\108\100\105\110\103 \084\111\111\108\115 \098\121 \070\051\088","\066\117\105\108\100\105\110\103 \084\111\111\108\115 \098\121 \070\051\088",Assets.PluginIcon)PluginButton.Click:Connect(function()PluginEnabled=not PluginEnabled PluginButton:SetActive(PluginEnabled)if PluginEnabled then Plugin:Activate(true)Enable(Plugin:GetMouse())else Disable()end end)Plugin.Deactivation:Connect(Disable)Selection.Changed:Connect(function()SelectionService:Set(Selection.Items)end)Enabling:Connect(function()Selection.Replace(SelectionService:Get())end)History.Changed:Connect(function()ChangeHistoryService:SetWaypoint("\066\117\105\108\100\105\110\103 \084\111\111\108\115 \098\121 \070\051\088")end)local J=Plugin:CreatePluginAction("\070\051\088\047\084\111\103\103\108\101\066\117\105\108\100\105\110\103\084\111\111\108\115","\084\111\103\103\108\101 \066\117\105\108\100\105\110\103 \084\111\111\108\115","\084\111\103\103\108\101\115 \116\104\101 \066\117\105\108\100\105\110\103 \084\111\111\108\115 \098\121 \070\051\088 \112\108\117\103\105\110\046",Assets.PluginIcon,true)J.Triggered:Connect(function()PluginEnabled=not PluginEnabled PluginButton:SetActive(PluginEnabled)if PluginEnabled then Plugin:Activate(true)Enable(Plugin:GetMouse())else Disable()end end)elseif Mode=="\084\111\111\108"then UIContainer=Player:WaitForChild("\080\108\097\121\101\114\071\117\105")Tool.Equipped:Connect(Enable)Tool.Unequipped:Connect(Disable)if not Tool.Parent then Disable()end Tool.AncestryChanged:Connect(function(J,s)if not s or not(s:IsA("\066\097\099\107\112\097\099\107")or s:IsA("\077\111\100\101\108")and Players:GetPlayerFromCharacter(s))then Disable()end end)end
-- Enable tool or plugin
if Mode == "Plugin" then
	-- Set the UI root
	UIContainer = CoreGui

	-- Create the toolbar button
	PluginButton = Plugin:CreateToolbar("Building Tools by F3X")
		:CreateButton("Building Tools by F3X", "Building Tools by F3X", Assets.PluginIcon)

	-- Connect the button to the system
	PluginButton.Click:Connect(function()
		PluginEnabled = not PluginEnabled
		PluginButton:SetActive(PluginEnabled)

		-- Toggle the tool
		if PluginEnabled then
			Plugin:Activate(true)
			Enable(Plugin:GetMouse())
		else
			Disable()
		end
	end)

	-- Disable the tool upon plugin deactivation
	Plugin.Deactivation:Connect(Disable)

	-- Sync Studio selection to internal selection
	Selection.Changed:Connect(function()
		SelectionService:Set(Selection.Items)
	end)

	-- Sync internal selection to Studio selection on enabling
	Enabling:Connect(function()
		Selection.Replace(SelectionService:Get())
	end)

	-- Roughly sync Studio history to internal history (API lacking necessary functionality)
	History.Changed:Connect(function()
		ChangeHistoryService:SetWaypoint("Building Tools by F3X")
	end)

	-- Add plugin action for toggling tool
	local ToggleAction = Plugin:CreatePluginAction(
		"F3X/ToggleBuildingTools",
		"Toggle Building Tools",
		"Toggles the Building Tools by F3X plugin.",
		Assets.PluginIcon,
		true
	)
	ToggleAction.Triggered:Connect(function()
		PluginEnabled = not PluginEnabled
		PluginButton:SetActive(PluginEnabled)

		-- Toggle the tool
		if PluginEnabled then
			Plugin:Activate(true)
			Enable(Plugin:GetMouse())
		else
			Disable()
		end
	end)
elseif Mode == "Tool" then
	-- Set the UI root
	UIContainer = Player:WaitForChild("PlayerGui")

	-- Connect the tool to the system
	Tool.Equipped:Connect(Enable)
	Tool.Unequipped:Connect(Disable)

	-- Disable the tool if not parented
	if not Tool.Parent then
		Disable()
	end

	-- Disable the tool automatically if not equipped or in backpack
	Tool.AncestryChanged:Connect(function(Item, Parent)
		if
			not Parent or not (
				Parent:IsA("Backpack") or (Parent:IsA("Model") and Players:GetPlayerFromCharacter(Parent))
			)
		then
			Disable()
		end
	end)
end
