Need Help on Texture Placing Plugin

Hey there everyone!

Recently, I was working on a build that required me to place a tileable texture around a building but I didn’t want to spend a ton of clicking on each part, inserting the texture (up to 4 times), tweaking what face it is placed on, etc. And so knowing nothing about how to make plugins, I tried. How it should work is that you select a texture object in the workspace, then click the place button. Then a highlight appears over every surface you are hovering over, then you can click to place it. Then you are able to reclick the place button to stop placing.

None of that happens, and I doon’t know why.

local changeHistoryService = game:GetService("ChangeHistoryService")
local selection = game:GetService("Selection")

local toolbar = plugin:CreateToolbar("Texture Tools")
local placeTextureButton = toolbar:CreateButton("Place Texture On Surface", "Place your selected texture on objects", "rbxassetid://4458901886")
local selectTextureButton = toolbar:CreateButton("Select Texture", "Select texture to place", "rbxassetid://4458901886")

placeTextureButton.ClickableWhenViewportHidden = false
selectTextureButton.ClickableWhenViewportHidden = false

local texture = nil
local connection = false
local surfaceHighlight

local mouse = plugin:GetMouse()

function selectTexture()
	local selectedObjects = selection:Get()
	if #selectedObjects == 1 and selectedObjects[1]:IsA("Texture") then
		texture = selectedObjects[1]
	end
end

function placeTexture()
	local function check(texture, part)
		if not part:FindFirstChild("TEXTURE") then
			return true
		elseif part:FindFirstChild("TEXTURE").Face ~= texture.Face then
			return true
		end

		return false
	end

	if connection == true and texture then
		if check(texture, mouse.Target) then
			local newTexture = texture:Clone()
			newTexture.Parent = mouse.Target
			newTexture.Name = "TEXTURE"
			newTexture.Face = mouse.TargetSurface
			
			changeHistoryService:SetWaypoint("placed surface")
		end
	end
end

function startPlacing()
	if connection == false and texture then
		connection = true

		surfaceHighlight = Instance.new("SurfaceSelection")
		surfaceHighlight.Color3 = Color3.fromRGB(172, 100, 17)
		surfaceHighlight.Transparency = 0.5
		surfaceHighlight.Parent = workspace

		while connection do
			task.wait()

			if mouse.Target then
				surfaceHighlight.Adornee = mouse.Target
				surfaceHighlight.TargetSurface = mouse.TargetSurface
			end
		end
	elseif connection == true and texture then
		connection = false
		texture = nil
	end
end

selectTextureButton.Click:Connect(selectTexture)
placeTextureButton.Click:Connect(placeTexture)

mouse.Button1Down:Connect(placeTexture)

Please let me know any ideas you have to fix it, I have no experience with plugins so help is really appreciated!