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

How do I stop the same map from being chosen twice?

Asked by 8 years ago

I have made an effort to try and figure it out and I can't. I'm hoping someone will be able to help me else I'll be stuck on this problem for awhile.

Here is the code. Does anyone have any suggestions to stop it from choosing the same map twice in a row?

function chooseMap()                    --Chooses Map (IN-PROGRESS)
    local mapChoice = math.random(1,3)
        if mapChoice == 1 then
            print("Map 1 got chosen!")
                local map = game.ServerStorage.Maps.Map1:clone()
                map.Parent = game.Workspace
                map:MoveTo(Vector3.new(100,100,100))
            --load map 1
        elseif mapChoice == 2 then
            print("Map 2 got chosen!")
                local map = game.ServerStorage.Maps.Map2:clone()
                map.Parent = game.Workspace
                map:MoveTo(Vector3.new(100,100,100))
            --load map 2
        elseif mapChoice == 3 then
            print("Map 3 got chosen!")
                local map = game.ServerStorage.Maps.Map3:clone()
                map.Parent = game.Workspace
                map:MoveTo(Vector3.new(100,100,100))
            --load map 3
    end 
end

1 answer

Log in to vote
4
Answered by
Legojoker 345 Moderation Voter
8 years ago

I tried to simplify the code along with your request. What I did is I added a condition that required the map to be different from the last map chosen, then setting that map to be the last map chosen.

local lastMap = 0
function ChooseMap()
    local mapChoice
    repeat mapChoice = math.random(1,#game.ServerStorage.Maps:GetChildren()) until mapChoice~=lastMap
    lastMap = mapChoice
    local map = game.ServerStorage.Maps["Map"..mapChoice]:Clone()
    -- Your loading code here.
end
0
Thank you so much! This is exactly what I was looking for. Also, thanks for simplifying it, that's helpful and also just taught me something. megamario640 50 — 8y
0
No problem! Legojoker 345 — 8y
Ad

Answer this question