Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I get all the connected parts of the same color?

As a side project I've been working on a bubble blaster. I'm having trouble figuring out how to get all the colors that are connected so that they can be "popped".

Here's an example of what I'm doing

I need to get all the colors that are connected and of the same color where there are more than three connected and "pop" them.

I've tried using magnitude on all the parts within a certain distance of the center bubble. I've also tried using a region3 and getting all the parts within it. I always end up getting a stack overflow error.

I'm not looking for someone to straight up write me a script. I'm looking for ideas on how I can accomplish my goal.

I don't currently have any code to show though I will write my previous attempts again if necessary.

2 answers

✓ Accepted answer

Ideas:

  • See if GetTouchingParts gets you all the parts you want (then just filter through them by color)
  • Go through all spheres and filter by color and magnitude (maximum magnitude should be slightly higher than the diameter of a sphere). Ideally you would have a more complicated algorithm that only looked at spheres in certain "sectors", but so long as the total number of spheres is not too high it shouldn't be too much of an issue.
  • Your Region3 idea is actually better than using magnitude, since Roblox knows better what parts are in what general areas. (You'll still want to use magnitude to make sure they're in range.)

So, your ideas are fine, but you note you had a stack overflow error. I'm going to guess you tried to get all the connected parts recursively (looking for a chain of, say, all the connected green spheres). There's nothing wrong with this, but you do need to include a table (as an argument to your recursive function) that contains all the spheres you've already looked at - otherwise you're going to be looking at the same two spheres over and over again!

Outline of avoiding stack overflow in recursion:

function recurse(sphere, adjacentList, seenDict) --only provide 'sphere' on first call
	adjacentList = adjacentList or {}
	seenDict = seenDict or {}
	seenDict[sphere] = true
	--find adjacent spheres here. Only recurse if same colour as sphere AND "not seenDict[sphere]"
	local adjacentSpheres --fill this in
	for i = 1, #adjacentSpheres do
		if adjacentSpheres[i].BrickColor == sphere.BrickColor and not seenDict[adjacentSpheres[i]] then
			table.insert(adjacentList, adjacentSpheres[i])
			recurse(adjacentSpheres[i], seenDict)
		end
	end
	return adjacentList
end

I think the way i'd go about doing this is using Raycasting. You could do a 360° raycast from the bubble that was last moved. Check the colors of the parts it hits, then recurse this function through nearby parts if they match.

Example;

local lastBlob = workspace.MainBlob; --This is the blob that moved

function CaptureSurroundingBlobs(blob)
	local matches = {}; --Table to capture the matching  blocks
	for i = 1,90 do --90*4=360
		local point = blob.CFrame --Rotate corresponding to iteration
					* CFrame.Angles(0,math.rad(i*4),0) 
					* CFrame.new(0,0,-(blob.Size.Z/2 + 5)).p
		local hit = workspace:FindPartOnRayWithIgnoreList(
			Ray.new(blob.CFrame.p,(point - blob.CFrame.p).unit*300),
			{unpack(matches)} --Ignore ones already accounted for
		);
		if hit then --Was there a hit?
			if hit.BrickColor == blob.BrickColor then --Same color?
				if not matches[hit.Name] then --Not already added?
					matches[hit.Name] = hit; --Add it
				end
			end
		end
	end
	return matches;
end

This discussion is preserved as it was. New answers, comments, and votes are closed.