Team Bring Command not working

Hello all, so I have been making a :teambring command where the command brings a whole team. However, when testing the command, the command is only able to bring the team “Neutral”. Any solutions? Script below.

local groupId = 15257297
local minRankToUse = 16

local function GetTeamPlayers(teamName)
	local plrTable = game.Players:GetChildren()

	for i, foundPlr in pairs(plrTable) do
		if foundPlr.Team == teamName then
			return foundPlr.Name
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	if plr:GetRankInGroup(groupId) >= minRankToUse then
		plr.Chatted:Connect(function(msg)
			local split = msg:split(" ")
			
			if split[1]:lower() == ":teambring" then
				local team = game.Teams:FindFirstChild(string.sub(msg, #split[1] + 1, #msg))
				local plrTable = GetTeamPlayers(team)
				game.Players[plrTable].Character.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame + Vector3.new(0, 10, 0)
			end
		end)
	end
end)

Script is in ServerScriptService and is a server script.

local CMDS = {
	[":teambring"] = function(Player:Player?, Team:Team?)
		local Team:Team? = game:GetService("Teams"):FindFirstChild(Team)
		if Team == nil then
			return nil
		end
		for _,v:Player? in Team:GetPlayers() do
			if v.Character then
				v.Character:MoveTo(Player.Character.PrimaryPart.Position)
			end
		end
	end,
}
game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Player:GetRankInGroup(groupId) >= minRankToUse then
			local Command = Message:lower():split(" ")
			CMDS[Command[1]](Player, Command[2])
		end
	end)
end)

Hi there, The problem with your code is that your team variable here is returning as nil

When a player has no team, otherwise known as neutral, when you try to index their team it returns nil. The reason that the neutral team is working is because when it checks to see if the teamName and the foundPlr.Team are equal to each other, it returns true because both values are nil. Currently you can type anything after :teambring and it will still teleport all neutral players.

To fix this, just got to make sure that the team variable is being assigned correctly. Also foundPlr.Team actually returns a Team object, so you need to make sure that when you compare the teamName string you compare it to the Team.Name rather than the object itself.

For my personal testing purposes, I made it so that you function returns a player object rather than just their name.


Here’s the updated code:

local groupId = 15257297
local minRankToUse = 16

local function GetTeamPlayers(teamName : string)
	local plrTable = game.Players:GetChildren()

	for i, foundPlr : Player in pairs(plrTable) do
		if foundPlr.Team and teamName and foundPlr.Team.Name:lower() == teamName:lower() then
			print("Team: ", foundPlr.Team)
			return foundPlr
		elseif teamName and not foundPlr.Team and "neutral" == teamName:lower() then
			return foundPlr
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	if plr:GetRankInGroup(groupId) >= minRankToUse then
		plr.Chatted:Connect(function(msg)
			local split = msg:split(" ")

			if split[1]:lower() == ":teambring" then
				local team = split[2]
				local foundPlrTable : Player = GetTeamPlayers(team)
				if foundPlrTable then
					foundPlrTable.Character.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame + Vector3.new(0, 10, 0)
				end
			end
		end)
	end
end)


Hope this helps! Let me know if you have any questions or if the code doesn’t work as intended.

1 Like

Hey! Thanks for this. It works perfectly. However, is it possible to change the team variable in the PlayerAdded function to the subbed string I had earlier? Some of the teams are not only one word.

Yep! Totally didn’t think about that haha. Just replace the line

with

local team = msg:split(":teambring ")[2]

that should allow you to target teams with spaces in their names.

This is brilliant! Thank you very much :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.