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.
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.
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)
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
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!
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
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
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.