So When The Countdown ends, The Both "Desert Map" and "RedMap" Load, I'm Scripting it so only one of them can load, But they load both on top of each other, Please Help!
local s = script.Stat local Players = game:GetService("Players") local PlayerCount = 0 game.Players.PlayerAdded:Connect(function() PlayerCount = PlayerCount + 1 end) game.Players.PlayerRemoving:Connect(function() PlayerCount = PlayerCount - 1 end) while PlayerCount < 1 do s.Value = "We Need More Players" repeat wait(1) until PlayerCount >= 1 end t = 0 while true do t = 8 repeat t = t-1 s.Value = "Map Loading in "..t wait(1) until t == 0 ------------------Game local Maps = {'DesertMap','RedMap'} local randomMap = math.random(1, #Maps) for i,v in pairs(Maps) do local storage = game:GetService("ReplicatedStorage") local map = storage.Maps:WaitForChild(v):clone() map.Parent = workspace.mapsInGame end t = 10 repeat t = t-1 s.Value = t.." seconds left" wait(1) until t == 0 workspace.mapsInGame:ClearAllChildren() end function teleAll(x, y, z) local posl = Vector3.new(game.Workspace.SpawnLocation) for m, plr in pairs(game:GetService("Players"):GetPlayers()) do if plr.Character then plr.Character:MoveTo(posl) end end end s.Value = "Game Ended!" wait(10)
On line 30, you're using for i,v in pairs(maps) do
. But you aren't actually choosing a map out of the two.
So in this situation we'll use a math.random
.
math.random's
are exceptionally useful to determine a random value, object or number. We can also use it for maps, case opening, etc.
A math.random
simply gets a random input out of the other values. Here is a quick example.
local numbers = {1, 2, 3, 4, 5} local numberChosen = math.random(1, #numbers) -- Choosing a random number
Doing this simply gathers a random number out of the numbers table, now I haven't used tables in a while, so that script may be wrong. But that is a little base of how it'd be used.
However with your script, we'll choose a random map, not a number.
for i,v in pairs(Maps) do local mapChosen = math.random(1, #Maps) local storage = game:GetService("ReplicatedStorage") local map = storage.Maps:WaitForChild(mapChosen):clone() map.Parent = workspace.mapsInGame end
And wuh-la!!
You have yourself a functioning Map Randomiser, well. Hopefully this helped, have a great rest of your day or night.
On the line 30 you typed "for i,v in ipair(maps) do" so you're get all children and now they're called v. On the line 32-33 you're cloning them (v) to the workspace , you're not choosing a random one.