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

In Game Script, it's occasionally making two maps?

Asked by
Tecara 0
10 years ago

It randomly selects one, and brings the one to workspace, on the first round there is always two maps, and the next rounds, there is occasionally one, or two.

function getRandomMap()
    local gameMaps = game.Lighting.Maps:GetChildren()
    local randomMap = gameMaps[math.random(1, #gameMaps)]
    return randomMap
end

function reshuffleteams(teams)

local players = game.Players:GetPlayers()
local t = true
for i = 1,#players do
        if t == true then
        players[i].TeamColor = game.Teams[teams[1]].TeamColor
        else
        players[i].TeamColor = game.Teams[teams[2]].TeamColor
        end
        t = not t
    end
end


function respawnnoobs()
    local P = game.Players:GetPlayers() 
    for i = 1,#P do
        P[i]:LoadCharacter()
    end

end

function startGame()
    if game.Players.NumPlayers >= 2 then
    gameStarted = true
    Workspace.RedTeam.Value = 1
    Workspace.BlueTeam.Value = 1
    local gameMap = getRandomMap()
    wait(3)
    local mapClone = gameMap:clone()
    mapClone.Parent = workspace
    mapClone.Name = "Map"
    reshuffleteams({"Goodish","Evilish"})
    respawnnoobs()
    timer = 0
    repeat wait(1)
        timer = timer+1
    until
    Workspace.BlueTeam.Value==0 or Workspace.RedTeam.Value==0 or timer==45

    if Workspace.BlueTeam.Value==0 then
h=Instance.new("Message", Workspace)
h.Text="RedTeam has won! Awarding Points.."


    elseif
         Workspace.RedTeam.Value==0 then
h=Instance.new("Message", Workspace)
h.Text="BlueTeam has won! Awarding Points.."


    else
         h=Instance.new("Message", Workspace)
h.Text="Timer has run out! Tie. No Points Awarded. D:"


    end
    wait(3)
    workspace:FindFirstChild("Map"):Destroy()
    reshuffleteams({"Lobby","Lobby"})
    respawnnoobs()
    workspace:FindFirstChild("Message"):Destroy()
    wait(30) ------------(intermission)
    startGame()
    else
    gameStarted = false

    end
    end




game.Players.PlayerAdded:connect(function()
    wait(10)
if game.Players.NumPlayers<2 and gameStarted == false then
h=Instance.new("Message", Workspace)
h.Text="Need more then 2 players to start"
game:GetService("Debris"):AddItem(h, 5) --Remove the message after 5 seconds.
elseif game.Players.NumPlayers >= 2 then
startGame()
end
end)

Why does it make two once in a while? Also any general improvement I could make to this script?

0
1, 35, 66 are the lines that deal with the maps. Tecara 0 — 10y

1 answer

Log in to vote
0
Answered by
wazap 100
10 years ago

Simple. Because when 2 ppl join the game startGame() gets called 2 times, which causes getRandomMap() to get called twice, hence resulting in 2 maps. I bet if 4 ppl joined the game at the same time there would be 4 maps in the next game.

0
Gah got it. knew it was gonna be a dumb mistake. so how should I change the script to still check for more than two players, but not repeat it twice? Tecara 0 — 10y
0
Take startGame() out of the PlayerAdded event, and also put startGame() in a while loop. What I'd do is in line 1 of the startGame() function put repeat wait() until game.Players.NumPlayers >= 2 wazap 100 — 10y
0
Found another way to fix it I ended up doing this: while wait(10) do if game.Players.NumPlayers<2 and gameStarted == false then h=Instance.new("Message", Workspace) h.Text="Need more then 2 players to start" game:GetService("Debris"):AddItem(h, 5) --Remove the message after 5 seconds. elseif game.Players.NumPlayers >= 2 then startGame() end end thanks for the help! Now I can finally go on. Tecara 0 — 10y
Ad

Answer this question