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

Random terrain gen problems?

So ive made this script where it duplicates 6x6 stud parts onto a 300x36 stud area. Every time a new part is duplicated, Var increases by 1. When Var reaches 300, the area stops generating. Then a function is activated so that each duplicant has its size increases randomly (in the y direction). The problem is, the output reads: bad argument #1 to 'pairs' (table expected, got nil). If you don't understand, please test the script for yourself:

Pos = game.Workspace.Model.Part.Position
x = 0
y = 0
z = 0
Var = 0
Var1 = 0
function Random()
	for i,v in pairs(Parts) do
	v.Size = Vector3.new(6,math.random(1,5),6)
	end
end
repeat
	wait(0.01)
	x = x + 6
	Var = Var + 1
	Var1 = Var1 + 1
	local NewPart = game.Workspace.Model.Part:Clone()
	NewPart.Anchored = true
	NewPart.Parent = game.Workspace.Model
	NewPart.Position = Pos + Vector3.new(x,y,z)
	if Var1==50 then
		Var1 = 0
		x = 0
		z = z + 6
	end
until Var==300 do
Random()
Parts = game.Workspace.Model:GetChildren()
end

1 answer

✓ Accepted answer

I noticed you created the table Parts after you called the function, when you should be creating it before.

Otherwise, the Random function is being called and is referring to a table that you have not created yet.

Pos = game.Workspace.Model.Part.Position
x = 0
y = 0
z = 0
Var = 0
Var1 = 0
function Random()
    for i,v in pairs(Parts) do
    v.Size = Vector3.new(6,math.random(1,5),6)
    end
end
repeat
    wait(0.01)
    x = x + 6
    Var = Var + 1
    Var1 = Var1 + 1
    local NewPart = game.Workspace.Model.Part:Clone()
    NewPart.Anchored = true
    NewPart.Parent = game.Workspace.Model
    NewPart.Position = Pos + Vector3.new(x,y,z)
    if Var1==50 then
        Var1 = 0
        x = 0
        z = z + 6
    end
until Var==300 do
Parts = game.Workspace.Model:GetChildren()-- Moved Up
Random() -- Moved Down
end


Let me know if you have any other questions or if that did not fix the issue.

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