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
1. The key word is not else if
, it's elseif
. One word. Knowing this, we can get rid of all those end
s...
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
You forgot to add a second number. math.random(1,10)
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