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

Why does this gamescript stop?!

Here is my script

local players = game.Players:GetPlayers()
local timeleft = Workspace:findFirstChild('time')
local currentMap = game.Workspace.currentMap
local _Teams = game:GetService'Teams'
local _Players = game:GetService'Players'


function removeTime()
    for i = 120, 0, -1 do
        wait(1)
        timeleft.Value = i
    end
end

function gameStart()
    local players = game.Players:GetPlayers()
    for i = 1, #players do
        local stats = players[i].PlayerGui.fullGui.everything.gamePlay.informer
        stats.Text = 'New round beginning!'
        stats.FontSize = 'Size24'
        timeleft.Value = 120
        wait(1)
        stats.Text = ''
        end
    end

function splitplayers()
	game.Teams.Lobby:remove()
	local Teams = _Teams:GetTeams()
	local function GetPlayers()
		return _Players:GetPlayers()
	end
	_Players.TeamColor = Teams[#GetPlayers()%#Teams].TeamColor
end

function pickteams()
local Cops = Instance.new("Team", game.Teams)
Cops.TeamColor = BrickColor.new("Bright blue") -- Note that TeamColor is a BrickColor value
Cops.Name = "Cops"
 
local Crimanals = Instance.new("Team", game.Teams)
Crimanals.TeamColor = BrickColor.new("Bright orange")
Crimanals.Name = "Crimanals"	
 end

function teleportPlayers()
    local players = game.Players:GetPlayers()
      
        local copspawn = Workspace.currentMap.CopBase.teleportationpart

		if players.TeamColor == BrickColor("Bright blue") then
			
			 players.Character.Torso.CFrame = CFrame.new(copspawn.Position)
                
		end
		
		 local robberspawn = Workspace.currentMap.RobberBase.teleportationpart2

		if players.TeamColor == BrickColor("Bright orange") then
			
			 players.Character.Torso.CFrame = CFrame.new(robberspawn.Position)
                
		end
           
            
            end
 



 
function waitSome()
    local players = game.Players:GetPlayers()
    for i = 1, #players do
        
        local stats = players[i].PlayerGui.fullGui.everything.gamePlay.informer
        stats.FontSize = 'Size24'
        stats.Text = "Let's take a quick break. Use this time to buy stuff in the shop!"
        wait(30)
        stats.Text = ''
        end
    end

 
function endGame()
	game.ReplicatedStorage.Lobby:Clone().Parent = game.Teams
    local players = game.Players:GetPlayers()
    for i = 1, #players do
        
        players[i]:LoadCharacter()
        end
    end
    currentMap:ClearAllChildren()

 
function goAhead()
    local players = game.Players:GetPlayers()
        for i = 1, #players do
            
            players[i].PlayerGui.fullGui.everything.gamePlay.informer.Text = 'We have enough players to start!'
           end
        end
        wait(5)


function chooseMap()
    local maps = game.ServerStorage.Maps
    local clone = maps[math.random(1,#maps)]
    clone.Parent = currentMap
end

 
while wait() do
    if game.Players.NumPlayers >= 2 then
        goAhead()
	splitplayers()
	pickteams()
        chooseMap()
        gameStart()
        teleportPlayers()
        removeTime()
        endGame()
        waitSome()
    else
        local players = game.Players:GetPlayers()
        for i = 1, #players do
           
            players[i].PlayerGui.fullGui.everything.gamePlay.informer.Text = '2 players are needed to start! Invite a friend.'
            end
        end
    end


The script so far works up until the gui text says "we have enough players" after that it is supposed to create teams and split the players into them and teleport them. HELP PLEASE! I dont know why this is happening i thought i tried everything.

1 answer

I noticed one problem in the function splitplayers. You are trying to set the TeamColor of Players, rather than each Player. I have no idea why the function uses the % operator and such, so here's my re-writing of it in a more readable/understandable fashion:

local function splitplayers()
	game.Teams.Lobby:Destroy()
	local UseSecondTeam = false
	for i, v in ipairs(_Players:GetChildren()) do
		if UseSecondTeam then
			v.TeamColor = _Teams.Criminals.TeamColor
		else
			v.TeamColor = _Teams.Cops.TeamColor
		end
		UseSecondTeam = not UseSecondTeam
	end
end

I can't guarantee this will work, but it should.

For this to work, you should also swap the calls to splitplayers and pickteams near the bottom.

2 comments

0

Do I replace [NameofFirstTeam] with the name of the first team in "" like this ["Cops"] or just "Cops" and after that it says TeamColor do i leave that or replace it with that teams color example "Bright blue" or just Bright blue

0

I updated the answer to add some more clarity; you should leave TeamColor as it is.

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