Need help creating a map generation system (SOLVED!)

I am trying to make a map generation system for my game. The issue is I do not know how to make it or how I would start.

What I mean by map generation system

What I mean by a map generation system is cloning models from ReplicatedStorage into Workspace with random positions inside a part. If possible, it would include a chance of something spawning in.

Please help, thanks!

Edit: After 7 days, we have finally solved this :slight_smile:

2 Likes

You would ideally want to split the map up into zones with building plots per zone that are loaded in, somethings like hills ect you would want to take up multiple zones for smoother looking terrain. Other than that all thats left would be to create buildings and then use a table to assign what building goes where, preferably you would want natural based zones to be seperate from city or town based otherwise you can end up with hills in the middle of a city. As for setting models you would simply need to set a basepart at the bottom center of each build and put it into the workspace and set the models primarypartcframe to the zone.

How would this go in a script to randomly generate it? Please show me a example. Also my game has a bunch of plots that I have in ReplicatedStorage to get cloned, and my game has only one area of plots.

You would get all the models e.g

local Buildings = game.replicatedstorage.Structures:GetChildren()



function AssignToPlots(GridX,GridY,Buildings)
 local grid = {}
    for i = 1,#GridX do
         for i2 =1,#GridY do
             Grid[GridX,GridY] = Buildings[math.random(1,#Buildings)]
          end
    end
end

So this example would put the buildings inti a grid format so you can load them to the correct positions
All that would be left is actually cloning the models and change the randomising to be chance.

My script does not seem to work, this is what it looks like now:

local Buildings = game.ReplicatedStorage.Plots:GetChildren()

function AssignToPlots(GridX,GridY,Buildings)
	local grid = {}
	for i = 1,#GridX do
		for i2 =1,#GridY do
			grid[GridX, GridY] = Buildings[math.random(150, #Buildings)] -- There is a error here.
		end
	end
end

AssignToPlots(game.Workspace.GridX, game.Workspace.GridY)
1 Like

The error is that you put 150 into math.random, math.random picks a random number between the first number given and the second. So #buildings is the second number as putting a hash before a variable containing a list will actuall give you the number of items in the list. So rather than 150 put 1

1 Like

Does not seem to work, I tried changing it from 150 to 1. I will give what the error is, maybe that will help.

Error from the script

Assigning 1 values to 2 variables initializes extra variables with nil; add ‘nil’ to value list to silence.

Any clue on how to fix it?

ChunkTutorial.rbxlx

credits: Profile - Razorboots - DevForum | Roblox

Read on this.

It is a good topic, but it does not fit my game. My game is split up into plots, like let’s say we have a table plot, kitchen and living room plot. The system would randomly generate out of those. That is what I am trying to say, sorry!

1 Like

maybe something like this can help you get started

local seed = nil -- if the seed is nil then it will be a random seed
local rand = Random.new(seed)
local chunks = game.ReplicatedStorage.Chunks:GetChildren()
local chunkSpacing = 16

for x = -10, 10 do
    for z = -10, 10 do
        -- select a random chunk model
        local i = rand:NextInteger(1, #chunks)
        -- clone the random chunk
        local clone = chunks[i]:Clone()
        -- position the clone chunk
        clone:PivotTo(CFrame.new(x * chunkSpacing, 0, z * chunkSpacing))
        -- parent the clone
        clone.Parent = workspace
    end
end

or if you want the chunks to be spawned at parts

local seed = nil -- if the seed is nil then it will be a random seed
local rand = Random.new(seed)
local chunks = game.ReplicatedStorage.Chunks:GetChildren()

for i, part in ipairs(workspace.Folder:GetChildren())
    -- select a random chunk model
    local i = rand:NextInteger(1, #chunks)
    -- clone the random chunk
    local clone = chunks[i]:Clone()
    -- position the clone chunk
    clone:PivotTo(part.CFrame)
    -- parent the clone
    clone.Parent = workspace
end

Does not seem to work, my script is in Workspace and it is a server-side Script.

Script:
local seed = nil -- if the seed is nil then it will be a random seed
local rand = Random.new(seed)
local chunks = game.ReplicatedStorage.Generation
local chunkSpacing = 11

for x = -10, 10 do
	for z = -10, 10 do
		-- select a random chunk model
		local i = rand:NextInteger(1, #chunks)
		-- clone the random chunk
		local clone = chunks[i]:Clone()
		-- position the clone chunk
		clone:PivotTo(CFrame.new(x * chunkSpacing, 0, z * chunkSpacing))
		-- parent the clone
		clone.Parent = game.Workspace
	end
end

Do you get any errors or can you see anything getting added to workspace?

1 Like

No errors, nothing added into Workspace.

I think the script is not running if you put

print("Hello")

At the top of the script does it print it when you run the game?

I also see a problem here

local chunks = game.ReplicatedStorage.Generation

Should be

local chunks = game.ReplicatedStorage.Generation:GetChildren()

I fixed it, also there is no print.

Still not working, any other way?

1 Like

Only localscripts don’t run when you put them in the workspace so if it’s a localscript then you have to put it into starterplayerscripts or replicatedfirst

I did tell you before, did you read correctly?

Then the only other option is the script is disabled if it’s not printing hello

The script is not disabled, not sure why this is not working. When I was testing I was using the Run feature in the testing menu, maybe that info will help.