How to make a "fill bucket"

Hello. Is it possible to make a fill bucket in Roblox, like most drawing applications have. Here is a reference:

I am trying to make this work but for parts - i.e you can draw a shape using parts, and fill the insides in with parts as well.
I currently have no idea how to make this into Roblox studio with parts, since unions are not supported with coding. Any help/advice/ideas will be extremely helpful, thank you!

1 Like

They actually are, albeit I wouldn’t recommend them anyways because of performance implications (and terrible topography too)

But I have an idea that only works if you’re using some sort of grid system to organize the parts/pixels/dots on the canvas, i.e. the parts can be referred to with a simple coordinate system.
The way it will work is when the player uses the bucket tool on a pixel, it will start a recursive cascade where it will keep looking for neighboring pixels that are of the same color as the initial one, essentially spreading like a virus.

7 Likes

Wow, that grid system looks promising! I am just wondering, would there be any performance issues with this?

Yes, it’s written on the top of the reply

I was talking about this option.

It should be fine if the area being painted isn’t humongous.

Hey, I’ve been using your idea but with instancing parts, and firing 4 raycasts from all directions to instance new parts. But the problem is that its super laggy, and the only way for it to stop lagging is if I add a wait(), which slows down the fill that i want to be instant. Would you know how to make this run quicker?

I mentioned something in my original reply:

Using raycasts to find neighboring parts is a terrible idea. What you should be doing instead is just adding or subtracting 1 from the pixel position to find them instead.

yeah, that would on a GUI however what im trying to make is a fill system using parts, and this is what I have rn:


As you can see it works, but theres a delay. Would you know how to remove this delay?

Here is my code:

script.Parent.ChildAdded:Connect(function(child)
	if child:IsA("BasePart") then
		if child.Name == "Territory" then
			local dir1,dir2,dir3,dir4 = Vector3.new(-2,0,0),Vector3.new(2,0,0),Vector3.new(0,0,-2),Vector3.new(0,0,2)
			local part1,part2,part3,part4
			wait()
			task.spawn(function()
				local result = workspace:Raycast(child.Position,dir1)
				if result == nil then
					part1 = Instance.new("Part")
					part1.Position = child.Position + dir1
					part1.Name = "Territory"
					part1.Size = Vector3.new(2,1,2)
					part1.Anchored = true

				end
			end)
			task.spawn(function()
				local result2 =  workspace:Raycast(child.Position,dir2)

				if result2 == nil then
					part2 = Instance.new("Part")
					part2.Position = child.Position + dir2
					part2.Name = "Territory"
					part2.Size = Vector3.new(2,1,2)
					part2.Anchored = true

				end
			end)
			task.spawn(function()
				local result3 =  workspace:Raycast(child.Position,dir3)

				if result3 == nil then
					part3 = Instance.new("Part")
					part3.Position = child.Position + dir3
					part3.Name = "Territory"
					part3.Size = Vector3.new(2,1,2)
					part3.Anchored = true

				end
			end)
			
			task.spawn(function()
				local result4 =  workspace:Raycast(child.Position,dir4)

				if result4 == nil then
					part4 = Instance.new("Part")
					part4.Position = child.Position + dir4
					part4.Name = "Territory"
					part4.Size = Vector3.new(2,1,2)
					part4.Anchored = true
				end
			end)
			
			if part1 then
				part1.Parent = script.Parent
			end
			if part2 then
				part2.Parent = script.Parent

			end
			if part3 then
				part3.Parent = script.Parent
			end
			if part4 then
				part4.Parent = script.Parent
			end
		end
	end	
end)

You’ve been ignoring what I said. You clearly have a grid system for the parts and you should use that to your advantage rather than using raycasts to find neighboring pixels, the latter of which is very impractical to use.

As for the delay, its because you have a wait() function.

1 Like

I’m not sure what you mean by a grid system?

The parts or pixels are organized into a grid. If you know the position of one pixel, you can easily determine what the neighboring pixels are by incrementing/decrementing the coordinates.

1 Like

Yeah I know that, but how do I know if a neighboring pixel already has a part? Won’t I need a raycast to check if there is a border to stop the filling?

You should know the coordinates of the border. Just have it stop spreading once the X and Z coordinates reach a certain number. For example, the borders might be at x=30, x=10, z=-30, z=10. All you need to do is check beforehand if the position of the pixel matches that of the borders.

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