To make a straightforward generation you first need to think of what you need. Let's strip down the problem to the base problem: we need to generate a blocky-like landscape. You can later implement biomes and types of blocks.
Reviewing your current code: there are some problems in it.
First of all you are looping over "blocks", a table which only has one member. That means that it will run it's code only once. I wouldn't do that, I would loop over an x and z range (the positions). To chose a block you can always use math.random to get a random block from the list.
Second: you are assigning Size to a Name. Size is a Vector3 and Name is a string. This will always conflict. I take that script.Parent.Grass is a brick: why not clone it?
The simplest terrain generator will just loop over all x and z positions and will assign a random height to it. You will see that the terrain will be extremely "peaky". If you run the code you will see what I mean. It is better to study random "noise" generators - I would suggest Perlin Noise.
local block = script.Parent.Grass
local function GetBlock()
return block:Clone()
end
local function GetSize()
return block.Size.z
end
local x_min, x_max = -100,100
local z_min, z_max = -100, 100
local y_min, y_max = 1,10
for x = x_min, x_max, GetSize() do
for z = z_min, z_max, GetSize() do
local height = math.random(y_min, y_max)
for y = GetSize()/2, GetSize() * (height + 0.5), GetSize() do
local block = GetBlock()
block.CFrame = CFrame.new(x,y,z)
block.Parent = game.Workspace
end
wait()
end
end
5 comments
Thanks. But is there a way to fix it? Or a way to understand it so 'I' can fix it? It doesn't connect and stuff. Try it and you will see
I see that my code is indeed incorrect - but you will see that it will work if the height of the block is exactly the same as the z size of the block. You will need to create a block (grass) which has Size.x == Size.y == Size.z. In that case it will work. Otherwise you have to edit the script so GetSize will return the size on the axis itself (GetSize("x")) so you can fix this problem. I tested i
So I have to make another grass block? If not, could you send me the new code and you can put comments on it.
Try it with a "Grass block" with Size = Vector3.new(4,4,4) (note: FormFactor must be Custom). You will see it will work.
i tried it by making it grass and with the size and the formfactor was custom. It didn't work