Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

The Map Randomly chooses but The Both Maps Load, Why?

Asked by
GentiRob -81
5 years ago

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)

0
this is a duplicated thread Imperialy 149 — 5y
0
i already answered in the previous 1 Imperialy 149 — 5y
0
I deleted it, Because the format was wrong, pls answer GentiRob -81 — 5y
0
format was the exact same. this is the 3rd time ive seen this. DinozCreates 1070 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

Hello, my name is TheOnlySmarts, and I'd be glad to help you with your problem.


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.


Tip : If this answer has helped you or somebody else you know, click 'Accept Answer' below to show love to the author, letting him/her know it has helped. It'll also give you both positive reputation, which is good for the community, as it brings a better understanding to a user!
Ad
Log in to vote
0
Answered by 5 years ago

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.

0
So how I choose a random one GentiRob -81 — 5y
0
this is clearly not your script. DinozCreates 1070 — 5y
0
How it's not my script? GentiRob -81 — 5y
0
So How Do I Choose a random map? GentiRob -81 — 5y
View all comments (3 more)
0
Use math.random. So do after the for i,v in pairs(maps) bla bla bla write local mapChosen = (1, #maps) . then add ur v thing to the random map. TheOnlySmarts 233 — 5y
0
Here I'll make an answer. TheOnlySmarts 233 — 5y
0
there, answer made, hopefully it helps, okay bubai :D TheOnlySmarts 233 — 5y

Answer this question