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

GameScript not spawning players?

My game script creates teams deletes the lobby team and puts the players on the teams but the problem is it doesn't teleport them to the teleportationparts in the map Please read to the end even tho this looks like alot.

local players = game.Players:GetPlayers()
local timeleft = Workspace:findFirstChild('timeleft')
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

local function splitplayers()
game.Teams.Lobby:Destroy()
local UseSecondTeam = false
for i, v in ipairs(_Players:GetChildren()) do
	if  UseSecondTeam then
		v.TeamColor = 
		_Teams["Cops"].TeamColor
	else
		v.TeamColor = 
		_Teams["Crimanals"].TeamColor
	end
		UseSecondTeam = not UseSecondTeam
end
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"
Cops.AutoAssignable = false
 
local Crimanals = Instance.new("Team", game.Teams)
Crimanals.TeamColor = BrickColor.new("Bright orange")
Crimanals.Name = "Crimanals"	
Crimanals.AutoAssignable = false
 end

function teleportPlayers()
    local players = game.Players:GetPlayers()
      
        local copspawn = workspace.teleportationpart

		if players.TeamColor == BrickColor("Bright blue") then
			
			 players.Character.Torso.CFrame = CFrame.new(copspawn.Position)
                
		end
		
		 local robberspawn = workspace.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 = "Intermission! Check our the shop and talk to your friends!"
        wait(30)
        stats.Text = ''
        end
    end

 
function endGame()
    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


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()
		pickteams()
		splitplayers()
		chooseMap()
        gameStart()
        teleportPlayers()
		if game.Workspace.NoCash == true then
		game.Workspace.timeleft = 0
        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
end


I think the problem is in the teleportplayers function but im not sure. It could in the end part where it calls all the functions. I know this looks lke a lot but its probly a simple fix.

1 answer

I see a few problems here:

Firstly, on line 115:

function chooseMap()
    local maps = game.ServerStorage.Maps
    local clone = maps[math.random(1,#maps)] --maps is not a table, it is just a reference to an object!
    clone.Parent = currentMap
end

I believe this is what you were going for:

function chooseMap()
    local maps = game.ServerStorage.Maps:GetChildren() -- "maps" is a Table of ServerStorage.Maps' descendants


    local clone = maps[math.random(1,#maps)]
    clone.Parent = currentMap
end

Also on line 128; if game.Workspace.NoCash == true then. I don't know if this is a BoolValue object, but if so, you need to add .Value.

Yes, you do have a problem with teleportPlayers. You put in the script players = game.Players:GetPlayers() and then players.TeamColor == BrickColor("Bright blue") when you just declared players to be a table of all children of game.Players. Like in your other functions, you need to iterate through each player and apply the code to each individual Player.

Also, when you are checking for a BrickColor, or CFrame, Vector2, Vector3, UDim2, you always still need to use .new because that is just how you refer to a BrickColor Value. So on lines 60 and 68, you need to use BrickColor.new().

Also, it seems on line 101, ClearAllChildren() didn't make it into your endGame() function.

Also, in the final function where you call everything, you need to make something wait for the game to end. if ____ then statements just run one time, at the time they are told to. What you need to use is a loop. That, and one of your end s is misplaced, making your else run equivalently to elseif not workspace.NoCash.Value then.

Also I changed the name of a few of your functions to make it easier for you to fix the ending function.

Another issue you had was in functions like waitSome() and gameStart() you had the code wait time for each individual player. What you want to use in those situations is spawn(), to run the wait times on a separate thread, meaning it can continue to the next player before the thread finishes.

-- Made by Bennymax3333

local timeleft = workspace:findFirstChild('timeleft')
local currentMap = workspace:findFirstChild('currentMap')
local _Teams = game:GetService'Teams'
local _Players = game:GetService'Players'


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

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

local function splitPlayers()
	_Teams.Lobby:Destroy()
	local UseSecondTeam = false
	for i, v in ipairs(_Players:GetPlayers()) do
		if UseSecondTeam then
			v.TeamColor = _Teams["Cops"].TeamColor
		else
			v.TeamColor = _Teams["Criminals"].TeamColor
		end
		UseSecondTeam = not UseSecondTeam
	end
end

function createTeams()
	local Cops = Instance.new("Team", _Teams)
	Cops.TeamColor = BrickColor.new("Bright blue") -- Note that TeamColor is a BrickColor value
	Cops.Name = "Cops"
	Cops.AutoAssignable = false

	local Criminals = Instance.new("Team", _Teams)
	Criminals.TeamColor = BrickColor.new("Bright orange")
	Criminals.Name = "Criminals"    
	Criminals.AutoAssignable = false
end

function teleportPlayers()

	-- Teleport Players to their spawn depending on which team they are on

	local players = _Players:GetPlayers()

	local copSpawn = workspace.teleportationpart
	local criminalSpawn = workspace.teleportationpart2


	for i = 1, #players do
		if players[i].TeamColor == BrickColor.new("Bright blue") then
			players.Character.Torso.CFrame = CFrame.new(copSpawn.Position)
		elseif players[i].TeamColor == BrickColor.new("Bright orange") then
			players.Character.Torso.CFrame = CFrame.new(criminalSpawn.Position)
		end
	end
end

function waitSome()
	local players = _Players:GetPlayers()
	for i = 1, #players do
		local stats = players[i].PlayerGui.fullGui.everything.gamePlay.informer
		spawn(function()
			stats.FontSize = 'Size24'
			stats.Text = "Intermission! Check our the shop and talk to your friends!"
			wait(30)
			stats.Text = ''
		end)
	end
end


function endGame()
	local players = _Players:GetPlayers()
	for i = 1, #players do
		players[i]:LoadCharacter()
	end
	currentMap:ClearAllChildren()
end



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


function chooseMap()
	-- Chooses a new map
	-- Stuff to Add: Make it so it removes the old map

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

-- So this ending function is a bit messed up
-- Remember how your game works:

-- [1] If there is enough players, start the game
-- [2] Wait for the game to finish
-- [3] Cleanup
-- [4] Intermission
-- Back to step [1]




while wait() do -- Every "server frame" (kind of, dn't hurt me ScriptingHelpers!)
	if _Players.NumPlayers >= 2 then
		goAhead() -- Tell all players we have enough players to start!
		createTeams() -- Create the Cops and Criminals teams
		splitPlayers() -- Assign each player to one of these teams
		chooseMap() -- Put a new map into currentMap
		gameStart() -- Tell everyone the game is starting and set timeleft to 120 seconds
		teleportPlayers() -- Teleport all players to their spawn

		-- We need to add something to wait for the round to end
		-- I recommend using a while true do loop like so:
		--[[
		
		while timeleft.Value > 0 do -- The round is still going
			wait()
		end
		-- Past here runs after the round ended





		]]

		if workspace.NoCash.Value then
			timeleft.Value = 0 --Should this be 120?
			countDown() -- Sets the time to 120 and counts down
			endGame() -- Respawn all Players, remove the currentMap
			waitSome() -- Wait 30 seconds for intermission
		end
	else -- If there is only one player, don't start the game
		local player = _Players:GetPlayers()[1]

		player.PlayerGui.fullGui.everything.gamePlay.informer.Text = '2 players are needed to start! Invite a friend.'
		
	
	end
end

I know I kind of answered a lot of issues you had, so if you have any specific questions about how or why things work, ask me or someone smarter than me.

3 comments

0

Thank you so much! I tried fix it for so long! All I knew was it wasn't teleporting the players! Thank you so much! I'll put your name in the script with mine to give credit. I wouldn't have been able to make it without you. I wish I could click the plus arrow to give you more rep but my rep is too low. Thanks a billion you just made my day!

0

Sure thing!

0

You can hit accept answer! That will give us both +1 reputation!

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