Need help creating a map generation system (SOLVED!)

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.

there is only 2 reasons i’m aware of that will prevent a script from running

  1. it does not have the correct Parent
  2. its disabled

other then them 2 reasons i have never seen a script not run

My script has the right parent and is not disabled. Is there any other way?

Can you send me a screenshot of where the script is and the scripts properties?