Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

1 comment

0

1, 35, 66 are the lines that deal with the maps.

1 answer

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.

3 comments

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?

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

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.