I am making an game. and i want to fill this 60x60 arena on the screenshot with parts sized 2x2x2
I wanna use an module to do this job but i am having issues, on how to actually do it because i don’t really know math . I decided to use pos1 and pos2 parts, so i can start spawning parts using these. POS1 starting and pos2 ending (i highlighted these parts on the arena screenshot)
If anyone wanna help me i will be soo grateful
local module = {}
function module.Generate()
local pos1 = workspace.POS1
local pos2 = workspace.POS2
local part = script.Snowpart
for i = 1,3600,1 do
local snowblock = part:Clone()
snowblock.Position = pos1
end
end
return module
You could try this, I’m not sure if its the best way but here you go.
local pos1 = workspace.POS1
local pos2 = workspace.POS2
local X, negativeX = math.floor((pos2.Position.X - pos1.Position.X) / 2), false
local Z, negativeZ = math.floor((pos1.Position.Z - pos2.Position.Z) / 2), false
if X < 0 then
X *= -1
negativeX = true
end
if Z < 0 then
Z *= -1
negativeZ = true
end
for i = 1, X do
local part1 = script.Snowblock:Clone()
if negativeX then
part1.Position = pos1.Position - Vector3.new(pos1.Size.X * i, 0, 0)
else
part1.Position = pos1.Position + Vector3.new(pos1.Size.X * i, 0, 0)
end
part1.Parent = workspace
task.wait()
for i = 1, Z do
local part2 = script.Snowblock:Clone()
if negativeZ then
part2.Position = part1.Position + Vector3.new(0, 0, part1.Size.Z * i)
else
part2.Position = part1.Position - Vector3.new(0, 0, part1.Size.Z * i)
end
part2.Parent = workspace
end
end
local TEMPLATE = script.Part
local WIDTH = TEMPLATE.Size.X
local module = {}
function module.Generate(startPos: Vector3, endPos: Vector3)
local container = Instance.new("Folder") -- For organization & easy cleanup
container.Name = "Blocks"
container.Parent = workspace
-- Fill based on the relative start/end positions
local xDelta = endPos.X - startPos.X
local zDelta = endPos.Z - startPos.Z
local rows = math.abs(xDelta) / WIDTH
local cols = math.abs(zDelta) / WIDTH
local xOffset = math.sign(xDelta) * WIDTH
local zOffset = math.sign(zDelta) * WIDTH
for x = 0, rows do
for z = 0, cols do
local part = TEMPLATE:Clone()
part.Position = startPos + Vector3.new(x * xOffset, 0, z * zOffset)
part.Parent = container
end
end
end
return module
local module = require(script.ModuleScript)
module.Generate(workspace.POS1.Position, workspace.POS2.Position)
Thank you both! They both work, but this one seems less complicated, but yeah both of them works. Though first one had an error and i fixed it myself. Ill use the second one. Thank you all!
I dont get why youre using starting and ending pos, you could do somethig like this:
local singlePartSize = Vector3.new(2,2,2)
local Xsize = 60
local Ysize = 1
local Zsize = 60
local pos1 = workspace.POS1.Position
for z = 0, Zsize/2 - 1, 1 do
for y = 0, Ysize - 1, 1 do
for x = 0, Xsize/2 - 1, 1 do
local part = Instance.new("Part", workspace.Cubes)
part.Position = Vector3.new(pos1.X + (x * 2) ,pos1.Y + (y * 2),pos1.Z + (z * 2))
part.Anchored = true
part.Size = singlePartSize
part.Shape = Enum.PartType.Block
end
end
end
sorry if i did it wrong and i dont see the arena screenshot
i have some trouble, no parts are spawning at all.
local module = {}
function module.Generate(startPos: Vector3, endPos: Vector3)
local Xsize = 60
local Ysize = 1
local Zsize = 60
local pos1 = workspace.POS1.Position
for z = 0, Zsize/2 - 1, 1 do
for y = 0, Ysize - 1, 1 do
for x = 0, Xsize/2 - 1, 1 do
local part = script.Snowblock:Clone()
part.Position = Vector3.new(pos1.X + (x * 2) ,pos1.Y + (y * 2),pos1.Z + (z * 2))
end
end
end
end
return module
The end position is useful for visualizing the area that will be generated. It’s also more intuitive to change the direction if needed since it will always fill between those positions.