Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Generating [closed]

Asked by 10 years ago

I am trying to make a Minecraft-like game on ROBLOX and I tried to make a generate script for grass. Here is what I tried:

grass = script.Parent.Grass
blocks = {grass}
grass.Anchored = true

for i,v in pairs(blocks) do
    generatedblocks = Instance.new("Part", Workspace)
    generatedblocks.Name = "Grass"
    generatedblocks.Size = grass.Name
    generatedblocks.Anchored = true
    generatedblocks.Position = -- I dont know what to put here and the rest.
end

Locked by JesseSong

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
1
Answered by
jobro13 980 Moderation Voter
10 years ago

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
-- Assing random position ranges
local x_min, x_max = -100,100
local z_min, z_max = -100, 100
local y_min, y_max = 1,10 -- this is in block units.
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() -- due the amount of recurses; wait, otherwise roblox will crash
    end
end
0
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 Randomplayer121 -2 — 10y
0
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 jobro13 980 — 10y
0
So I have to make another grass block? If not, could you send me the new code and you can put comments on it. Randomplayer121 -2 — 10y
0
Try it with a "Grass block" with Size = Vector3.new(4,4,4) (note: FormFactor must be Custom). You will see it will work. jobro13 980 — 10y
0
i tried it by making it grass and with the size and the formfactor was custom. It didn't work Randomplayer121 -2 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

Well I'm not quite sure what your asking because I know you can generate random terrain but I'm not quite sure about generating Grass Blocks even though it seems simple enough.

Well I could help you with setting the position it would have to be using the code Vector3.new(0,0,0) and you would have to find a way to keep adding that up evenly, throughout the base plate. You could also use Loop if that might give you any ideas? Not quite sure how this script would work but it is interesting.