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

Why won't the player spawn?

Im using the script below and it works just find other then that the players dont actually spawn on the correct teams spawn. From testing it, it just choses a random spawn and spawns the player there. Does anybody know why and how to fix it?

local plr = game.Players.LocalPlayer
_G.teamNumber1 = 0
_G.teamNumber2 = 0
local team1 = BrickColor.new('Bright blue')
local team2 = BrickColor.new('Bright red')

function Set()
if _G.teamNumber1 > _G.teamNumber2 then
plr.TeamColor = team2
_G.teamNumber2 = _G.teamNumber2 + 1
elseif
_G.teamNumber1 < _G.teamNumber2 then
plr.TeamColor = team1
_G.teamNumber1 = _G.teamNumber1 + 1
elseif
_G.teamNumber1 == _G.teamNumber2 then
--do math.random or watever u like, I'll just put him to Team1
plr.TeamColor = team1
_G.teamNumber1 = _G.teamNumber1 + 1
end
plr.Neutral = false
end

script.Parent.MouseButton1Click:connect(function()
Set()
plr:LoadCharacter()
end)

1 comment

0

Do the spawns have their TeamColor set properly, and also set to not Neutral?

1 answer

Your Problem

What you have should work for spawning on a team's spawn. You should check that the TeamColor (and not just BrickColor) is set on the SpawnLocations, and also that Neutral is false on the SpawnLocation.

Fixing _G

The way you're using _G won't be useful.

_G is local to each client, so the numbers will pretty much always be 0 (even if they weren't local, each time a player spawns you're setting everything back to 0!)

Instead of trying to keep track of changes, just go ahead and count players by their teamcolor whenever you need them.

Here's a teamSize function I just wrote which looks through all players and looks at their Neutral and TeamColor properties, giving a count:

function teamSize(team)
	local players = game.Players:GetPlayers()
	local count = 0
	if team then
		-- Count not neutral
		for _, player in pairs(players) do
			if not player.Neutral and player.TeamColor == team then
				count = count + 1
			end
		end
	else
		-- Count neutral
		for _, player in pairs(players) do
			if player.Neutral then
				count = count + 1
			end
		end
	end
	return count
end

Now instead of reading the global variables, you just ask for the team size of the two teams.

local LocalPlayer = game.Players.LocalPlayer

local team1 = BrickColor.new("Bright blue")
local team2 = BrickColor.new("Bright red")

function Set()
	local size1 = teamSize(team1)
	local size2 = teamSize(team2)
	if size1 < size2 then
		LocalPlayer.TeamColor = team1
	elseif size2 < size1 then
		LocalPlayer.TeamColor = team2
	else
		-- Or randomly
		LocalPlayer.TeamColor = team1
	end
	LocalPlayer.Neutral = false
end

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