Instance.new Teams

So, I am using Instance.new to create a team and I need to figure out how to create the team with Instance.new and then put tools into it based on the name of the team.

I’ve found no other solution on how to do it so I came here to figure it out.

All help is appreciated.


local TeamService = game:GetService("Teams");
local PlayerService = game:GetService("Players");
local ServerStorage = game:GetService("ServerStorage");


local RedTeamTools = ServerStorage:WaitForChild("ReadTeamTools");
-- A folder inside of ServerStorage that contains all the tools you want red team to have


local RedTeam = Instance.new("Team");
RedTeam.Parent = Teams;
RedTeam.TeamColor = BrickColor.Red();
--This is creating the red team and parenting it to teams


PlayerService.PlayerAdded:Connect(function(Player)--Everytime a players joins the following code executes
	Player.CharacterAdded:Connect(function(Char)--So now every time a player is respawned the following code executes
		if Player.Team == RedTeam then -- Checking to see if player is on the correct team for tools
			for k,v in ipairs(ReadTeamTools:GetChildren()) do -- Putting all of the tools into a table and begining loop 
				v:Clone().Parent = Player.Backpack; -- Cloning tool at index(k) of the table and putting it into the players backpack
			end
		end
	end)
end)
3 Likes

Alright so this is my script.

Script
local teamEvent = game:GetService("ReplicatedStorage"):WaitForChild("ApplyTeam")
local Teams = game:GetService('Teams'):GetChildren()
local GroupService = game:GetService("GroupService")
local teamEvent = game.ReplicatedStorage.ApplyTeam
local TextService = game:GetService("TextService")

local TeamService = game:GetService("Teams");
local PlayerService = game:GetService("Players");
local ServerStorage = game:GetService("ServerStorage");


local TeamTools = ServerStorage:WaitForChild("Tools");
-- A folder inside of ServerStorage that contains all the tools you want red team to have


local AMERICA = 5387760


teamEvent.OnServerEvent:Connect(function(plr,name,teamcolor, Id)
		local team = game.Teams:FindFirstChild(name)
	
		
if team then
	if plr:GetRankInGroup(Id) > 1 or plr:GetRankInGroup(AMERICA) >= 254 then
		plr.Team = team
		plr.Character.Head.RankGui.Frame.RANK.Text = plr:GetRoleInGroup(Id) 
		plr.plrStats.TeamId.Value = Id
		plr.Character.Humanoid.Health = "0"
	end
		
else
	if plr:GetRankInGroup(Id) > 1 or plr:GetRankInGroup(AMERICA) >= 254 then
		local team = Instance.new("Team")
		team.Name = name
		local existingTeam = game.Teams:FindFirstChild(name)
		team.TeamColor = teamcolor
		team.Parent = game.Teams
		team.AutoAssignable = false
		plr.Team = team
		plr.Character.Humanoid.Health = "0"
		plr.plrStats.TeamId.Value = Id
		plr.Character.Head.RankGui.Frame.RANK.Text = plr:GetRoleInGroup(Id) 
		
	end
end


if team then
	if plr:GetRankInGroup(AMERICA) == 0 then
		plr.Team = team
		plr.Character.Head.RankGui.Frame.RANK.Text = "Visitor"
		plr.plrStats.TeamId.Value = Id
		plr.Character.Humanoid.Health = "0"
		
	else
		local team = Instance.new("Team")
		team.Name = name
		local existingTeam = game.Teams:FindFirstChild(name)
		team.TeamColor = teamcolor
		team.Parent = game.Teams
		team.AutoAssignable = false
		plr.Team = team
		plr.Character.Humanoid.Health = "0"
		plr.plrStats.TeamId.Value = Id
	end
	
end
	
end)

 PlayerService.PlayerAdded:Connect(function(Player,name,Id)
	local team = game.Teams:FindFirstChild(name)
	Player.CharacterAdded:Connect(function(Char)
		if Player.Team == team then 
			for k,v in ipairs(TeamTools.Id:GetChildren()) do 
				v:Clone().Parent = Player.Backpack; 
			end
		end
	end)
end)

and here is a picture of the folder.
410dbadf2559dfb41441200d8c050b3f

What’s the error you’re having with that script? I can see a few places that look odd here.


PlayerService.PlayerAdded:Connect(function(Player)--Everytime a players joins the following code executes
	Player.CharacterAdded:Connect(function(Char)--So now every time a player is respawned the following code executes
		if Player.Team == RedTeam then -- Checking to see if player is on the correct team for tools
			for k,v in ipairs(ReadTeamTools:GetChildren()) do -- Putting all of the tools into a table and begining loop 
				v:Clone().Parent = Player.Backpack; -- Cloning tool at index(k) of the table and putting it into the players backpack
			end
		end
	end)

tools dont go into the team

:joy: Well you have ReadTeamTools instead of RedTeamTools.

PlayerService.PlayerAdded:Connect(function(Player,name,Id)
	local team = game.Teams:FindFirstChild(name)
	Player.CharacterAdded:Connect(function(Char)
		if Player.Team == team then 
			for k,v in ipairs(TeamTools.Id:GetChildren()) do 
				v:Clone().Parent = Player.Backpack; 
			end
		end
	end)

You may want to do TeamTools[Id] instead of TeamTools.Id. The latter looks for a child of TeamTools that’s explicitly called “Id”:

Hi. Could you try with this piece of code instead? It worked for me in the studio:

local team = game.Teams:FindFirstChild(name)
local Id = "5511008"

team.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		if Player.Team == team then 
			for k, v in ipairs(TeamTools[Id]:GetChildren()) do 
				v:Clone().Parent = Player.Backpack 
			end
		end
	end)
end)

This checks whenever a player spawns in the team rather than in the whole server. Also, remember to define every argument you use in a function.

2 Likes

That would only work if I put it under the remote event which is only fired when the team button is pressed.