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

Why won't this math.random script work?

So I am making this minigame place. I am trying to make selecct minigames randomly. I have 10 maps. But why won't this work?

local number = math.random(10)
if number = 1 then
game.ReplicatedStorage.Map1:clone()
else if number = 2 then
game.ReplicatedStorage.Map2:clone()
else if number = 3 then
game.ReplicatedStorage.Map3:clone()
else if number = 4 then
game.ReplicatedStorage.Map4:clone()
else if number = 5 then
game.ReplicatedStorage.Map5:clone()
else if number = 6 then
game.ReplicatedStorage.Map6:clone()
else if number = 7 then
game.ReplicatedStorage.Map7:clone()
else if number = 8 then
game.ReplicatedStorage.Map8:clone()
else if number = 9 then
game.ReplicatedStorage.Map9:clone()
else if number = 10 then
game.ReplicatedStorage.Map10:clone()
end
end
end
end
end
end
end
end
end
end

3 comments

0

It's elseif, not else if unless you're going into a new if then statement.

0

What he has is correct, it's just not what you're supposed to do. elseif avoids the massive pile of ends.

0

Whatever you're trying to do, it doesn't sound like a client-side operation. If everyone is going to be on the map, you're going to do this server-sided.

3 answers

✓ Accepted answer

1. The key word is not else if, it's elseif. One word. Knowing this, we can get rid of all those ends...

local number = math.random(10)
if number = 1 then
	game.ReplicatedStorage.Map1:clone()
elseif number = 2 then
	game.ReplicatedStorage.Map2:clone()
elseif number = 3 then
	game.ReplicatedStorage.Map3:clone()
elseif number = 4 then
	game.ReplicatedStorage.Map4:clone()
elseif number = 5 then
	game.ReplicatedStorage.Map5:clone()
elseif number = 6 then
	game.ReplicatedStorage.Map6:clone()
elseif number = 7 then
	game.ReplicatedStorage.Map7:clone()
elseif number = 8 then
	game.ReplicatedStorage.Map8:clone()
elseif number = 9 then
	game.ReplicatedStorage.Map9:clone()
elseif number = 10 then
	game.ReplicatedStorage.Map10:clone()
end

2. You must understand that = and == are not the same thing; you have = the assignment operator. This is what you use to give a variable or a property a specific value. Like this:

a = .5 --Assignment operator, =
Instance.new("Part", workspace).Transparency = a --Assignment operator, =

And then you have ==, the equal to (or whatever it's called) operator, to compare values. Like this:

a = 2 --Assignment, =
b = 2 --Assignment, =

if a + b == 4 then --Equal to, ==
	print(a.." + "..b.." == "..a + b)
end

Let's apply that to your script:

local number = math.random(10)
if number == 1 then
	game.ReplicatedStorage.Map1:clone()
elseif number == 2 then
	game.ReplicatedStorage.Map2:clone()
elseif number == 3 then
	game.ReplicatedStorage.Map3:clone()
elseif number == 4 then
	game.ReplicatedStorage.Map4:clone()
elseif number == 5 then
	game.ReplicatedStorage.Map5:clone()
elseif number == 6 then
	game.ReplicatedStorage.Map6:clone()
elseif number == 7 then
	game.ReplicatedStorage.Map7:clone()
elseif number == 8 then
	game.ReplicatedStorage.Map8:clone()
elseif number == 9 then
	game.ReplicatedStorage.Map9:clone()
elseif number == 10 then
	game.ReplicatedStorage.Map10:clone()
end

3. There is a way to shorten this script quite a bit. Here it is shortened:

local number = math.random(10)
game.ReplicatedStorage["Map"..number]:Clone()

So, we have a new way to access objects. I don't know if you're familiar with this method, but you can access, say, game.Workspace.Part like this:

game["Workspace"]["Part"]

...with brackets and quotation marks. Although it takes more energy to write, it will sometimes give us a little more control over how we access things, and in this scenario, bring a 22 line script down to about 2 lines.

4. I'm just going to assume that this is a server script trying to access stuff from ReplicatedStorage. Only local scripts are meant to access ReplicatedStorage, and only server scripts are meant to access ServerStorage. You're best off moving all your maps to ServerStorage and accessing the maps from there:

local number = math.random(10)
game.ServerStorage["Map"..number]:Clone()

5. You should probably store your map clone in a variable.

local number = math.random(10)
local map = game.ServerStorage["Map"..number]:Clone()

From there you can control the clone of the map. If your intent is to put it in the workspace, you can do that easily:

local number = math.random(10)
local map = game.ServerStorage["Map"..number]:Clone()
map.Parent = workspace

2 comments

0

Um... I was using a local script?

2

o god we forgot to put the parent, I'll let you my place your awnser is better >.>

You forgot to add a second number. math.random(1,10)

1 comment

0

If only one number is given, then the script will automatically find a number between 1 and the number given.

Just put all the maps in a model in "ServerStorage" called "Maps" and put a model in Workspace called "MapHolder"

local timer = --Put time here for how long the map should be removed
while true do
	game.Workspace.MapHolder:ClearAllChildren()
	local maps = game.ServerStorage:WaitForChild("Maps"):GetChildren()
	local clone = maps[math.random(1, #maps)]:Clone()
	clone.Parent = game.Workspace.MapHolder
	wait(timer)
end

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