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

How would I do this more efficiently?

So, here's my problem. I am in the process of making a mining game and the system I am using works just fine but I am having a little trouble creating the blocks. Whenever a block is being mined, the server creates blocks around the one being mined. Now, the way I find out what blocks to make where, I put all the surrounding Vector3 Values in a table and then compare them to another table containing all the blocks:

local pp=part.Position
	local sides={}
	sides[1]=pp+Vector3.new(2,2,2)
	sides[2]=pp+Vector3.new(0,2,2)
	sides[3]=pp+Vector3.new(-2,2,2)
	sides[4]=pp+Vector3.new(2,2,0)
	sides[5]=pp+Vector3.new(0,2,0)
	sides[6]=pp+Vector3.new(-2,2,0)
	sides[7]=pp+Vector3.new(2,2,-2)
	sides[8]=pp+Vector3.new(0,2,-2)
	sides[9]=pp+Vector3.new(-2,2,-2)
	sides[10]=pp+Vector3.new(2,0,2)
	sides[11]=pp+Vector3.new(0,0,2)
	sides[12]=pp+Vector3.new(-2,0,2)
	sides[13]=pp+Vector3.new(2,0,0)
	sides[14]=pp+Vector3.new(0,0,0)
	sides[15]=pp+Vector3.new(-2,0,0)
	sides[16]=pp+Vector3.new(2,0,-2)
	sides[17]=pp+Vector3.new(0,0,-2)
	sides[18]=pp+Vector3.new(-2,0,-2)
	sides[19]=pp+Vector3.new(2,-2,2)
	sides[20]=pp+Vector3.new(0,-2,2)
	sides[21]=pp+Vector3.new(-2,-2,2)
	sides[22]=pp+Vector3.new(2,-2,0)
	sides[23]=pp+Vector3.new(0,-2,0)
	sides[24]=pp+Vector3.new(-2,-2,0)
	sides[25]=pp+Vector3.new(2,-2,-2)
	sides[26]=pp+Vector3.new(0,-2,-2)
	sides[27]=pp+Vector3.new(-2,-2,-2)

This is the way I figure out where to check for blocks to place (note: each block is 2x2x2). The way I typed it up is very time consuming and gives me a headache trying to keep track of the values to make sure I don't mess up or forget one. Is there a better way of doing this with a loop and can it be extended for more layers of blocks?

2 answers

✓ Accepted answer

You're just interested in all of the spaces with +-2 in x, y, and z. It's pretty easy to write this as 3 for loops:

for x = -1, 1 do
	for y = -1, 1 do
		for z = -1, 1 do
			print(x, y, z)
		end
	end
end

This lets you look at every neighbor:

-1	-1	-1
-1	-1	0
-1	-1	1
-1	0	-1
-1	0	0
-1	0	1
-1	1	-1
-1	1	0
-1	1	1
0	-1	-1
0	-1	0
0	-1	1
0	0	-1
0	0	0
0	0	1
0	1	-1
0	1	0
0	1	1
1	-1	-1
1	-1	0
1	-1	1
1	0	-1
1	0	0
1	0	1
1	1	-1
1	1	0
1	1	1

There's a hiccup here that you have to (depending on what you're doing) ignore 0, 0, 0.

If for some reason it's easier still to build the sides list instead of doing stuff inside the loop with a function call, it would look like this

for x = -1, 1 do
	for y = -1, 1 do
		for z = 1, 1, do
			if x ~= 0 or y ~= 0 or z ~= 0 then
				-- (not 0,0,0)
				table.insert(sides, part.Position + Vector3.new(x, y, z) * 2)
			end
		end
	end
end

3 comments

0

Thank you so much! I have been wondering an easier way to do this but I didn't quite know how or where to start. Is there a way to figure out not only the blocks just outside the first but the ones after that as well? For example: 2-1-0-1-2 (not just around the first layer but also a second layer)

1

Wait a sec, would it be just as easy as for x=-2,2 do ?

0

Yes, yes it was XD

-- Made by Niles :D
-- Just edit dist
local dist = 2


local genTable = function(pp)
	local tab = {}

	-- Generate Table!
	for a = -dist, dist, dist do
		for b = -dist, dist, dist do
			for c = -dist, dist, dist do
				tab[#tab + 1] = pp + Vector3.new(a, b, c)
			end
		end
	end
	return tab
end

local sides = genTable(part.Position) -- Done!

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