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

How to pick/ spawn a map randomly?

local ReplicatedStorage = game:GetService("ReplicatedStorage")

function loadMap()
  	local maps = ReplicatedStorage.GameFolder.MapFolder:GetChildren()
  	local MapAmount =#maps
	local Chosen = math.random(1,MapAmount)
  	local map = maps = ReplicatedStorage.GameFolder.MapFolder[map..Chosen..]
  		map:Clone()
  		map.Parent = game:GetService("Workspace")
  		map:MakeJoints()
  
end

Can somebody fix this for me, its got something to do with tables, and stuff i think, im not familiar with this, so any help would be appreciated.

2 answers

✓ Accepted answer

Your code looks good until line 07.

This line chooses a random map from your MapFolder, which fixes your line 07.

local map = ReplicatedStorage.GameFolder.MapFolder[math.random(1, #maps)]

This is the finished code.

function loadMap()
	local maps = game:GetService("ReplicatedStorage").GameFolder.MapFolder:GetChildren()
	local map = ReplicatedStorage.GameFolder.MapFolder[math.random(1, #maps)]

        map:clone().Parent = game:GetService('Workspace')
        map:MakeJoints()
end
function loadMap()
	-- this is a table indexed from 1 to n (where n is number of children) 
	-- of the children of the map folder
	local maps = game:GetService("ReplicatedStorage").GameFolder.MapFolder:GetChildren();
	-- math.random(number) returns a random number between 1 and number.
	-- hence it is perfect for picking a random index in our maps folder
	local map = maps[math.random(#maps)];
	-- now we clone and place the map
	map:Clone();
	map.Parent = game:GetService("Workspace");
	map:MakeJoints();
end;

I think that's what you're looking for?

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