I’m relatively new to the DevForum, so I may have grammar or other issues!
-
What do you want to achieve? I want to make a terrain generator similar to Minecraft, but using Hexagons!
-
What is the issue? My generator script makes an error, namely:
attempt to compare number <= nil
-
What solutions have you tried so far? I am using @ThanksRoBama’
s script from this post.
I modified their script so that it could work with hexagons, but it always return an error. this is the script:
local FractalNoise = require(game.ServerStorage.FractalNoise)
local heightmapNoise = FractalNoise(nil, 0.1, 2, 2, nil, nil) --These are just magic numbers
local swirlNoise = FractalNoise(nil, .01, 5, 2) --These are just magic numbers
function mapSet(map, x, y, z, value)
map[x] = map[x] or {}
map[x][y] = map[x][y] or {}
map[x][y][z] = value
end
function mapGet(map, x, y, z)
if map[x] then
if map[x][y] then
return map[x][y][z]
end
end
end
function twirlCoordinates(x, y, z, power)
local power = power or 1
local tX, tY, tZ =
swirlNoise(x, y, z),
swirlNoise(x+1000, y, z), --Don't want the *same* twirl on each axis
swirlNoise(x, y+1000, z)
return x + tX * power, y + tY * power, z + tZ * power
end
function heightMap(x, z)
return heightmapNoise(x, 0, z)
end
function density(x, y, z)
--If you twirl with power 0, you'll just get a plain heightmap
local tX, tY, tZ = twirlCoordinates(x, y, z, 1)
tZ = tZ / (1 + y)
tX = tX / (1 + y)
local densityOffset = 0.5 + heightMap(tX, tZ) - y --Add 0.5 density so that there's a guaranteed bottom layer
return densityOffset
end
function generateChunk(mapSize, offset)
local yMax = 8
local yMin = 1
print(offset)
local x
local y
local z
local densityMap = {}
for x = 1, mapSize do
for y = yMin, yMax do
for z = 1, mapSize do
mapSet(densityMap, x*offset["x"], y, z*offset["z"], density((x*offset["x"])/mapSize, y/(yMax), (z*offset["z"])/mapSize))
end
end
end
x, y, z = 0,0,0
for x = 1, mapSize do
for y = yMin, yMax do
for z = 1, mapSize do
local d = mapGet(densityMap, x*offset["x"], y, z*offset["z"])
--print(d, x*offset["x"], y, z*offset["z"])
if d >= 0 and d <= 0.14 then
local block = workspace.blockBase.Base_Plane:Clone()
--local block = Instance.new("Part", workspace)
--block.Size = Vector3.new(4, 4, 4)
if z % 2 == 0 then
x += 3.732 * 0.5
end
z *= 3.732 * 0.75
block.Anchored = true
block.CFrame = CFrame.new((x*offset["x"])*4, y*4, (z*offset["z"])*3.732)
block.Color = Color3.fromHSV(((y/mapSize)%1), .75, 1 - (y/mapSize))
block.Parent = game.Workspace
end
end
end
game["Run Service"].Heartbeat:Wait()
end
x, y, z = 0,0,0
end
generateChunk(16,
["x"] = 1,
["z"] = 1
})
If you need the module, just get it from the link I gave.
Also, if you have questions, feel free to ask!