Decreasing paint tool lag

so i’ve created a paint tool for painting a grid of studs, but the brush size system i’ve made so far is pretty inefficient.


as you can see in this video, there’s a considerably delay between when the mouse is actually clicked (the circle turns neon green) and when the parts are actually painted (obvious), which although the delay is incredibly little time, it still makes the tool feel clunky and slow regardless. i want it to feel smooth, and i know a couple games with similar paint tools that are pretty much what i want to make but they’ve figured out a solution to all the lag, so i know there has to be a better way.

these are the functions responsible for painting the parts:

local script

			for _, v in workspace:GetPartsInPart(cursor, params) do
				if isTargeting and v.Color == targetColor then
			table.insert(partsInCursor, v)
			local outline = Instance.new("SelectionBox")
				outline.Parent = v
				outline.LineThickness = .02
			outline.Adornee = v
				outline.Color3 = color
			outline.Name = "Outline"
		elseif not isTargeting then
			table.insert(partsInCursor, v)
			local outline = Instance.new("SelectionBox")
			outline.Parent = v
			outline.LineThickness = .02
			outline.Adornee = v
			outline.Color3 = color
			outline.Name = "Outline"
				end
		end

-- later in the script
			elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
				stopPaint = false
				cursor.Material = Enum.Material.Neon
					while not stopPaint do
					tool.Paint:FireServer(partsInCursor, color, targetColor, isTargeting)
					wait()
					end

the main thing i want to potentially do is reduce the amount of events i have to fire, but i just can’t think of a way to do so.

server script:

script.Parent.Paint.OnServerEvent:Connect(function(player, paintParts, color, targetColor, isTargeting)
	print(#paintParts)
		if #paintParts > 0 then
		for _, v in paintParts do
			if v.Color == color then
				return
			end
			print(v)
			if isTargeting then
					v.Color = color
			end
		end
		end
end)

if anyone has a better solution to adding brush sizes to my paint tool, i’d like to hear it.
thanks in advance

A good option for a tool like thay would be to add a que, if you have a table with all the positions when they are clicking you can then go through that table and paint with the brush size, this means that with a coroutine you can cut out all the jitters and this also helps control lag if the player is laggin anywau

so would i put the serversided stuff in a coroutine or the local stuff

You could do it on either side, you either feed the items from the que into the server and paint it there or keep the que on the server, if you want to be more efficient on client to server communication you can even send multiple in one to the server and keep the que there. This would also allow for a smaller wait time on the client cutting out any skipping e.c.t