How to make team only gear?

Title, I put down 2 pads, and once a player steps on the pad they switch teams, and get a weapon according to which team they join. Only problem is, how do I do this??

5 Likes
local Tools = {
    ["Team .."] = -- Tool here,
    ["Team .."] = -- Tool here
}
 
	plr.CharacterAdded:Connect(function()
    if not plr.Team then
        return
    end
    local tool = Tools[plr.Team.Name]
    if tool then
        tool:Clone().Parent = plr:WaitForChild("Backpack")
	end
end)

Just change the team name and the tool position at the top.

1 Like

For the tool here, do I do something like game.Workspace.axe or do you mean something else by saying that.

1 Like

Just type the location of the tool there, e.g workspace.axe .

1 Like

Do I have to create a variable for plr??

1 Like

No, you could just do

local Player = game:GetService("Players")
Player.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
    if not plr.Team then
        return
    end
    local tool = Tools[plr.Team.Name]
    if tool then
        tool:Clone().Parent = plr:WaitForChild("Backpack")
	end
2 Likes

Do I have to fill anything in for local tool = Tools[plr.Team.Name] like do i fill in the team and everything?

2 Likes

No, since its all defined at the top.

1 Like

Personally I recommend making it detect any tools within the team via :GetChildren() and then assigning them to the player. Make the script less dependant and needs no editing.

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		for i,v in pairs(game:GetService("Teams"):GetChildren()) do
			if v.TeamColor == Player.TeamColor then
				for _,v in pairs(v:GetChildren()) do
					if v:IsA("Tool") then
						v:Clone().Parent = Player:WaitForChild("Backpack")
					end
				end
			end
		end		
	end)
end)
3 Likes

My code is more beginner friendly, maybe you will like it (just put this Script in ServerScriptService):

    game:GetService("Players").PlayerAdded:Connect(function(player)
    	player.CharacterAdded:Connect(function(character)
    		for _, v in pairs(game:GetService("Teams"):GetChildren()) do
    			if v:IsA("Team") and player.Team == v then
    				for _, v2 in pairs(v:GetChildren()) do
    					if v2:IsA("Tool") then
    						v2:Clone().Parent = player:WaitForChild("Backpack")
    					end
    				end
    			end
    		end
    	end)
    end)

To make it work you need to put Tools inside of a Team, like this:
example3

Here’s a test Baseplate I have created for this: Team Only Gear.rbxl (23.0 KB)

Here are the results:

Edit: here’s a bit better script:

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local team = player.Team
		if team then
			for _, v in pairs(team:GetChildren()) do
				if v:IsA("Tool") then
					v:Clone().Parent = player:WaitForChild("Backpack")
				end
			end
		end
	end)
end)
7 Likes

I think the reason it isn’t working for me is because they start on neutral team, and then pick by stepping on a platform. I probably have to loop it so it keeps checking.

1 Like

Essentially every time their team changes this code will run.

local TeamsService = game:GetService("Teams")

for _, team in pairs(TeamsService:GetTeams()) do
	team.PlayerAdded:connect(function(player)
		for i,v in pairs(game:GetService("Teams"):GetChildren()) do
			if v.TeamColor == Player.TeamColor then
				for _,v in pairs(v:GetChildren()) do
					if v:IsA("Tool") then
						v:Clone().Parent = Player:WaitForChild("Backpack")
					end
				end
			end
		end	
	end)
end
1 Like

Do not forget about removing stuff that is already in Backpack, Character:

for _, team in pairs(game:GetService("Teams"):GetTeams()) do
	team.PlayerAdded:Connect(function(player)
        local character = player.Character or player.CharacterAdded:Wait()
        for i, v in pairs(character:GetChildren()) do
              if v:IsA("Tool") then
                    v:Destroy()
              end
        end
        for i, v in pairs(player:WaitForChild("Backpack"):GetChildren()) do
              if v:IsA("Tool") then
                    v:Destroy()
              end
        end
		for i, v in pairs(game:GetService("Teams"):GetChildren()) do
			if v.TeamColor == player.TeamColor then
				for i2, v2 in pairs(v:GetChildren()) do
					if v:IsA("Tool") then
						v2:Clone().Parent = player:WaitForChild("Backpack")
					end
				end
			end
		end	
	end)
end

Put this as a Script in ServerScriptService (I believe that you will still need to use my previous Script).

2 Likes

It doesn’t work either, probably because it only checks once when a player joins. How can I make it check over and over, because you can switch your team mid game.

1 Like

This is what I have.

local Tools = {
	["Napoleonic"] = game.Teams.Napoleonic.Musket,
		["Medieval"] = game.Teams.Medieval.Axe
}

local TeamsService = game:GetService("Teams")
local Player = game.Players.LocalPlayer
for _, team in pairs(TeamsService:GetTeams()) do
	team.PlayerAdded:Connect(function(player)
		local character = player.Character or player.CharacterAdded:Wait()
		for i, v in pairs(character:GetChildren()) do
			if v:IsA("Tool") then
				v:Destroy()
			end
		end
		for i, v in pairs(player:WaitForChild("Backpack"):GetChildren()) do
			if v:IsA("Tool") then
				v:Destroy()
			end
		end
		for i, v in pairs(game:GetService("Teams"):GetChildren()) do
			if v.TeamColor == player.TeamColor then
				for _,v in pairs(v:GetChildren()) do
					if v:IsA("Tool") then
						v:Clone().Parent = Player:WaitForChild("Backpack")
					end
				end
			end
		end	
	end)
end

,but i get an error.

 ServerScriptService.Team-Only Gear Script:25: attempt to index nil with 'WaitForChild'
1 Like
local Tools = {
	["Neutral"] = {
		script.Tool,
	},
	["Blue"] = {
		
	},
	["Red"] = {
		
	},
}


local function UpdateTeamTools(Player)
	if Player.Team == nil then return end
	Player.Backpack:ClearAllChildren()
	local TeamTools = Tools[Player.Team.Name]
	if TeamTools then
		for i = 1, #TeamTools do
			TeamTools[i]:Clone().Parent = Player.Backpack
		end
	end
end


game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		UpdateTeamTools(Player)
	end)
	Player:GetPropertyChangedSignal("Team"):Connect(function()
		UpdateTeamTools(Player)
	end)
end)
6 Likes

Well, you can change your team mid game after joining by stepping on a pad, but your script only checks once they join (i think) so that’s why I dion’t think it works.

1 Like

Did you even try the script? or read it?, it updates the tools when the character is added or when the team is changed…

Yes, but it does not work? Well, what now?

Oh, and this is what I have.

local Tools = {
	["Neutral"] = {
		
	},
	["Napoleonic"] = {
        game.Teams.Napoleonic.Musket
	},
	["Medieval"] = {
     game.Teams.Medieval.Axe
	},
}


local function UpdateTeamTools(Player)
	if Player.Team == nil then return end
	Player.Backpack:ClearAllChildren()
	local TeamTools = Tools[Player.Team.Name]
	if TeamTools then
		for i = 1, #TeamTools do
			TeamTools[i]:Clone().Parent = Player.Backpack
		end
	end
end


game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		UpdateTeamTools(Player)
	end)
	Player:GetPropertyChangedSignal("Team"):Connect(function()
		UpdateTeamTools(Player)
	end)
end)