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

Random map generation script not working!?

So, I have no idea what the error is this time.. D: So here it is:

RoundStart = game.Workspace.RoundStart  
Room1 = game.ServerStorage.Room1
Room2 = game.ServerStorage.Room2
Room3 = game.ServerStorage.Room3
Room4 = game.ServerStorage.Room4
Room5 = game.ServerStorage.Room5

function NewMap()
	if RoundStart.Value == true then
		RandomRoom = (math.random(1,5))
		if RandomRoom == 1 then
			local Map1 = Room1:Clone()
			Map1.Parent = game.Workspace
		end
		if RandomRoom == 2 then
			local Map2 = Room2:Clone()
			Map2.Parent = game.Workspace
		end
		if RandomRoom == 3 then
			local Map3 = Room3:Clone()
			Map3.Parent = game.Workspace
		end
		if RandomRoom == 4 then
			local Map4 = Room4:Clone()
			Map4.Parent = game.Workspace
		end
		if RandomRoom == 5 then
			local Map5 = Room5:Clone()
			Map5.Parent = game.Workspace
		end
	end
end

RoundStart.Value.Changed:connect(NewMap)

Please help?

5 comments

0

You can find the error in the output

0

Commonly, if this script is usually initiated once the Player joins the game, ServerStorage wouldn't have loaded yet. I usually recommend using Lighting.

0

There is no error in the output.

0

It doesn't matter if RoundStart is being changed. If It helped, accept the answer. We are not supposed to give you a full done script.(I got in trouble for it :)

0

It didn't help me at all........ :P

2 answers

I recommend using lists/tables when dealing with multiple objects:

RoundStart = game.Workspace.RoundStart
--Initialize Rooms list
numRooms = 5
Rooms = {}
for i = 1, numRooms do
	Rooms[i] = game.ServerStorage["Room" .. i]
end

function NewMap()
	if RoundStart.Value then
		local randomRoom = math.random(1, 5)
		local map = Rooms[randomRoom]:Clone()
		map.Parent = workspace
	end
end

RoundStart.Value.Changed:connect(NewMap)

As you can see, using a list makes the NewMap function considerably smaller.

You might want to add RoundStart.Value = false somewhere in the NewMap function (within the 'if RoundStart.Value then` block).

4 comments

0

You could also do table.insert instead of doing Rooms[i] = etc, blah, blah, blah.

0

True, but table.insert is a couple times slower than Rooms[i] = ___ (at least when I tested it just now) and gives no benefit in this case (since we know the length of the table is i-1)

0

It is better to do, "local test = 0". Incase of a new roblox update, many things might change. For example they remove the Lighting and the only way to reach it is by doing GetService or something. Better safe than sorry.

0

BTW if RandomRoom then would work since RandomRoom is a searched by a "FindFirstChild" method...

  1. Make the variables local
  2. Is RoundStart being changed?

Local Variables

You need to do this because of this example.

local num = 0 --Variable

--What you are doing is this

test = 0 --This would mean that the variable test will be changed to 0. But in order to do this
--"test" will need to be an already existing variable.

--To let the script know you are making a new variable, put local behind it.

num = 1 --This will work since num was created on line 1.

Is the the RoundStart value being changed? Instead of using a value, call the function in the script.

function startround()
	print("Starting round!")
end

startround() --Calling the function right here in the script!


Here is the perfected script, that may need some fixing. You can fix the rest.

function NewMap()
       local  RandomRoom = game:GetService("ServerStorage"):FindFirstChild("Room"..math.random(1,5))
        	if RandomRoom then
            		RandomRoom:clone().Parent = workspace
		end
        end
end



Hope this helps!

2 comments

0

RoundStart is being changed...

0

Your comment on line 5 in the first script is false. "test = 0" is perfectly legal. Since "test" doesn't exist beforehand, it's considered a global variable. It's still a good idea to use 'local' when possible, but it's not required. Also, if "RandomRoom" is nil in line 3 (last script), the NewMap function will fail -- I'd recommend throwing an error or simply not checking "if RandomRoom then" at

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