How Could My 2D Random "Landscape" Generator Be Improved?

Hello DevForum!
A few days ago I managed to make a working 2D “landscape” generator, and I was wondering how it could be improved, currently it works fine but I already know it can be improved, on a side note, I would like to add more features to it, in which case I will reply with the new code in this thread.
Code:

local part = workspace.Part

local xOffset = 1
local startingHeight = Vector3.new(0,0,0)


local function CreateCubes()
	local startingCube = Instance.new("Part")
	startingCube.Position = startingHeight
	startingCube.Size = Vector3.new(1,1,1)
	startingCube.Anchored = true
	startingCube.Color = Color3.new(0,0,0)
	startingCube.Material = Enum.Material.Neon
	startingCube.Parent = workspace
	print("starting cube created")
	local oldCube = startingCube
	while true do
		local newCube = Instance.new("Part")
		newCube.Position = Vector3.new(xOffset,oldCube.Position.Y + math.random(-1,1),0)
		if newCube.Position.Y >= 100 then
			newCube.Position = Vector3.new(xOffset,oldCube.Position.Y + math.random(-1,0), 0)
		elseif newCube.Position.Y <= -100 then 
			newCube.Position = Vector3.new(xOffset,oldCube.Position + math.random(0,1),0)
		end
		if newCube.Position.Y >= -15 and newCube.Position.Y <= 15 then
			newCube.Color = Color3.new(0.371252, 0.745892, 0.370169)
			newCube.Material = Enum.Material.Grass
		elseif newCube.Position.Y >= -30 and newCube.Position.Y <= -15 then
			newCube.Color = Color3.new(0.394614, 0.394614, 0.394614)
			newCube.Material = Enum.Material.Rock
		elseif newCube.Position.Y >= -60 and newCube.Position.Y <= -30 then
			newCube.Color = Color3.new(0.203494, 0.203494, 0.203494)
			newCube.Material = Enum.Material.Rock
		elseif newCube.Position.Y >= -100 and newCube.Position.Y <= -60 then
			newCube.Color = Color3.new(0.570199, 0.202441, 0.0988327)
			newCube.Material = Enum.Material.CrackedLava
		elseif newCube.Position.Y >= 15 and newCube.Position.Y <= 30 then
			newCube.Color = Color3.new(0.74905, 0.74905, 0.74905)
			newCube.Material = Enum.Material.Rock
		elseif newCube.Position.Y >= 30 and newCube.Position.Y <= 60 then
			newCube.Color = Color3.new(0.919417, 0.919417, 0.919417)
			newCube.Material = Enum.Material.Snow
		elseif newCube.Position.Y >= 60 and newCube.Position.Y <= 100 then
			newCube.Color = Color3.new(0.638529, 0.827054, 1)
			newCube.Material = Enum.Material.Ice
		end
		newCube.Size = Vector3.new(1,1,1)
		newCube.Anchored = true
		newCube.Parent = workspace
		print("new cube created")
		oldCube = newCube
		xOffset += 1
		wait(1/60)
		print("restarting")
	end
end

local function DestroyPart()
	wait(tick)
	part:Destroy()
end

part.Touched:Connect(CreateCubes)
part.Touched:Connect(DestroyPart)

I also noticed a few things with this generator, 1: when I set the rate at 1/120, it actually goes at the same speed, and when I put the wait time to destroy the part to 1, it actually makes a “shotgun” effect.
I also know the method I put to trigger this isn’t the best but it was the only one I could think of.

1 Like

These should use Vector3.zero and Vector3.one respectively.

Constants should be used for this entire thing to make it less of a nightmare to read. Also, why are you setting newCube.Position, and then reading it? Why not just compute the Y-component, and use that? It would be a lot shorter.

task.wait should be used here.

Didn’t know about their existance, now I do

What do you mean, variables?

Because I set it trougg some automatic way and then check if it’s over 100 below -100 so then I fix it (it’s a limit)

I don’t understand sorry

Ops

Anyway thanks, I’ll come back with the updated code when I can

I updated the code following your advice, except the Y-constant which I didn’t understand, here’s the code:

local part = workspace.Part

local xOffset = 1
local startingHeight = Vector3.zero

local green = Color3.new(0.371252, 0.745892, 0.370169)
local grey = Color3.new(0.394614, 0.394614, 0.394614)
local darkGrey = Color3.new(0.203494, 0.203494, 0.203494)
local red = Color3.new(0.570199, 0.202441, 0.0988327)
local lightGrey = Color3.new(0.74905, 0.74905, 0.74905)
local white = Color3.new(0.919417, 0.919417, 0.919417)
local azure = Color3.new(0.638529, 0.827054, 1)

local function CreateCubes()
	local startingCube = Instance.new("Part")
	startingCube.Position = startingHeight
	startingCube.Size = Vector3.one
	startingCube.Anchored = true
	startingCube.Color = Color3.new(0,0,0)
	startingCube.Material = Enum.Material.Neon
	startingCube.Parent = workspace
	print("starting cube created")
	local oldCube = startingCube
	while true do
		local newCube = Instance.new("Part")
		newCube.Position = Vector3.new(xOffset,oldCube.Position.Y + math.random(-1,1),0)
		if newCube.Position.Y >= 100 then
			newCube.Position = Vector3.new(xOffset,oldCube.Position.Y + math.random(-1,0), 0)
		elseif newCube.Position.Y <= -100 then 
			newCube.Position = Vector3.new(xOffset,oldCube.Position + math.random(0,1),0)
		end
		if newCube.Position.Y >= -15 and newCube.Position.Y <= 15 then
			newCube.Color = green
			newCube.Material = Enum.Material.Grass
		elseif newCube.Position.Y >= -30 and newCube.Position.Y <= -15 then
			newCube.Color = grey
			newCube.Material = Enum.Material.Rock
		elseif newCube.Position.Y >= -60 and newCube.Position.Y <= -30 then
			newCube.Color = darkGrey
			newCube.Material = Enum.Material.Rock
		elseif newCube.Position.Y >= -100 and newCube.Position.Y <= -60 then
			newCube.Color = red
			newCube.Material = Enum.Material.CrackedLava
		elseif newCube.Position.Y >= 15 and newCube.Position.Y <= 30 then
			newCube.Color = lightGrey
			newCube.Material = Enum.Material.Rock
		elseif newCube.Position.Y >= 30 and newCube.Position.Y <= 60 then
			newCube.Color = white
			newCube.Material = Enum.Material.Snow
		elseif newCube.Position.Y >= 60 and newCube.Position.Y <= 100 then
			newCube.Color = azure
			newCube.Material = Enum.Material.Ice
		end
		newCube.Size = Vector3.one
		newCube.Anchored = true
		newCube.Parent = workspace
		print("new cube created")
		oldCube = newCube
		xOffset += 1
		task.wait(1/60)
		print("restarting")
	end
end

local function DestroyPart()
	wait(tick)
	part:Destroy()
end

part.Touched:Connect(CreateCubes)
part.Touched:Connect(DestroyPart)

For the second wait task.wait() breaks the script, had to remove it

1 Like
...
local yPosition = oldCube.Position.Y + math.random(-1, 1)
if yPosition >= 100 then
    yPosition = oldCube.Position.Y + math.random(-1, 0)
else if yPosition <= -100 then
    yPosition = oldCube.Position.Y + math.random(0, 1)
end
newCube.Position = Vector3.new(xOffset, yPosition, 0)
if yPosition >= -15 and yPosition <= 15 then
...
1 Like

Oh, I’ll try it that next time, it does look good now, I’ll show the code when I can, thanks!