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

Terrain generates in the Negative Z And X Axis but not the Positive Z And X Axis?

local amplitude = 1
local frequency = 10
local lacunarity = 2
local gain = 2.2
local octaves = 8
local seedobj = game.Workspace:WaitForChild("UserSeed")
local seed = seedobj.Value
local chunkSize = 128

local chunkOffset = chunkSize/2

-- Fractal Brownian Motion:
function FBM(x, z)
	local y = 0
	local amp = amplitude
	local freq = frequency
	for i = 1,octaves do
		y = (y + (amp * math.noise((x + seed) / freq, (z + seed) / freq)))
		freq = (freq * lacunarity)
		amp = (amp * gain)
	end
	return y
end

function GenerateHeightmap(size, x, z)

	-- Create 2D heightmap:
	local heightmap = {}
	for x = x,size do
		local hx = {}
		heightmap[x] = hx
		for z = z,size do
			-- Generate a perlin noise value using FBM:
			local y = FBM(x, z)
			hx[z] = y
			
			local cframesize = CFrame.new(1, 1, 1)
			local cframepos = CFrame.new(x, y, z)
			
			if cframepos.Y <= -16 then
				game.Workspace.Terrain:FillBlock(CFrame.new(x, -28 - 50, z), Vector3.new(1,100,1), Enum.Material.Water)
				game.Workspace.Terrain:FillBlock(CFrame.new(cframepos.X, cframepos.Y-10, cframepos.Z), Vector3.new(1,10,1), Enum.Material.Sand)
			else
				game.Workspace.Terrain:FillBlock(CFrame.new(cframepos.X, cframepos.Y-10, cframepos.Z), Vector3.new(1,10,1), Enum.Material.Grass)
			end
			
		end
	end
end

-- Generate heightmap with a size of chunkSize

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		while true do
			local hum = Character:WaitForChild("HumanoidRootPart")
			
			local xv = hum.Position.X
			local zv = hum.Position.Z

			local heightmap = GenerateHeightmap(chunkSize, xv - chunkOffset, zv - chunkOffset)
			
			print(chunkSize, xv - chunkOffset, zv - chunkOffset)
			
			wait(10)
		end	
	end)
end)

1 answer

in you function GenerateHeightmap(size, x, z) thers a little error in the area your generating loop. its fine when you spawn the size of the chunk stays at 128x128 or starts at your position minus 64 in both x and z.

But as you move around these values or if you like the chunk size changes

Example you move 5 blocks in the positive X the for loop will start from the position you give minus the size via your vx - ChunkOffset Plus 5 (the studs you've walked in the X plane)

the way i would correct this is the following:

function GenerateHeightmap(size, x, z)

    -- Create 2D heightmap:
	local heightmap = {}
	for x = x,x+size do -- need to add the original X Value to the size otherwise the area we're generating will grow causing lag!
      		local hx = {}
		heightmap[x] = hx
		for z = z,z+size do -- same with Z!
		-- etc.....

I've tried the new code and as i walk it starts at negitive values for x and z but it does change to positive if i walk far enough.

Hope this helps! :)

Note: Editited due to making it more readable!

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