Team Clothes Script

Hey there!
What I’m trying to accomplish is to have all players on a certain team spawn with certain clothes, but only players on that team. I’ve looked on Google, Youtube, forum, etc, but I can’t find anything that isn’t outdated on this. How would I do this? I don’t want to have a giver part, either. Any and all help is appreciated!

3 Likes

Create a table of teams like this:

local Table = {
	["Team Name"] = {
		Shirt = "ShirtId";
		Pants = "PantId"

	}}

And then use a playeradded/characteradded event to check what Team and then change those clothes in the character.

2 Likes

I’m still just learning onPlayerAdded, and I’m not sure how I would use that to detect the player’s team.

onPlayerAdded isn’t an actual thing. It’s just the common name of a function that a lot of youtubers use.

To use a player/character added event, you can do something like this:

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
--stuff
end)
end)

Great! Now we need to check if they are on a team.

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
if player.Team == game.Teams[""] then --put team name in quotation
--stuff
        end
    end)
end)

Now that we have that code written down, we need to swap out the clothing asset IDs for the new ones.

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
--divider
if player.Team == game.Teams[""] then 
character:FindFirstChildOfClass("Shirt").ShirtsTemplate = "rbxassetid://" .. 00 --put shirt id here
character:FindFirstChildOfClass("Pants").PantsTemplate = "rbxassetid://" .. 00 --put shirt id here
        end
--divider
    end)
end)

There you go! The Team and clothing asset ids are for you to configure.
Additionally, you can copy and paste the code inside the "–divider"s if you want more than one team to have certain clothing.

I have 0 experience with clothing instances so let me know if there are any errors.

2 Likes

Also if you don’t want a lot of if statements in your script its better to loop through a table and check what team. Because a lot of if statements can get messy. Feel free to use mine or alxzile’s.

local TeamClothes = {
	["Island"] = {
		Shirt = "http://www.roblox.com/asset/?id=6018717643";
		Pants = "http://www.roblox.com/asset/?id=6018659870";
	};
	["Yes"] = {
		Shirt = "http://www.roblox.com/asset/?id=6018717643";
		Pants = "http://www.roblox.com/asset/?id=5954847816";
	}
}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Shirt = character:WaitForChild("Shirt")
		local Pants = character:WaitForChild("Pants")
		for teamName,Clothes in pairs(TeamClothes) do
			if teamName == tostring(player.Team) then
				for _,ID in pairs (TeamClothes) do
					Shirt.ShirtTemplate = Clothes.Shirt
					Pants.PantsTemplate = Clothes.Pants
				end
			end
		end
	end)
end)
4 Likes

This just made the character’s clothes not load in on either team…

Did you make sure the name of the teams in the script match the team names in your game?

Yep. I did that. I have no idea why it isn’t working.

The script looks fine, I’ll test it. But before I do, can you check if the asset ids work in studio by putting them on a character manually? If so let me know and I’ll test out the script.

Yep. I tested the IDs, and they both work fine normally.

I am fixing the script one sec.

This new script should work,

llocal TeamClothes = {
	["Island"] = {
		Shirt = "6028056507";
		Pants = "6018659870";
	};
	["Yes"] = {
		Shirt = "3599544679";
		Pants = "419419645";
	}
}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local ShirtModel
		local PantsModel
		for teamName,Clothes in pairs(TeamClothes) do
			if teamName == tostring(player.Team) then
				local success, err = pcall(function()
					ShirtModel = game.InsertService:LoadAsset(Clothes.Shirt)
					PantsModel = game.InsertService:LoadAsset(Clothes.Pants)
				end)
				if success then
					character.Shirt:Destroy()
					character.Pants:Destroy()
					ShirtModel.Shirt.Parent = character
					PantsModel.Pants.Parent = character
				end
			end
		end
	end)
end)

Tested it and it works.

1 Like
function addClothing(player, character)
	repeat wait() until player:HasAppearanceLoaded() -- make sure the character has loaded before proceeding
        -- Set new shirt and pants variables in case the user isn't wearing any shirts or pants by default
	local Shirt
	local Pants
	if not character:FindFirstChildOfClass("Shirt") then
		Shirt = Instance.new("Shirt")
		Shirt.Parent = character
	elseif character:FindFirstChildOfClass("Shirt") then
		Shirt = character:FindFirstChildOfClass("Shirt")
	end
	if not character:FindFirstChildOfClass("Pants") then
		Pants = Instance.new("Pants")
		Pants.Parent = character
	elseif character:FindFirstChildOfClass("Pants") then
		Pants = character:FindFirstChildOfClass("Pants")
	end

	for teamName, clothes in pairs(TeamClothes) do -- "teamName" is name of team. "clothes" is the table with the Pants and Shirts
		if player.Team.Name == teamName then
			if Shirt then Shirt.ShirtTemplate = clothes.Shirt end
			if Pants then Pants.PantsTemplate = clothes.Pants end -- if statements aren't necessary, but is a safe-to-have.
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	if player.Character then addClothing(player, player.Character) end -- call addClothing() in case code is running after the character has already loaded.
	player.CharacterAdded:Connect(function(character)
		addClothing(player, character)
	end)
end)

One thing to note is that the shirt assets you provided didn’t load on my character so I would definitely double check the id’s you provided in the table. Otherwise, this script does its job. Hopefully this helped! I learned a lot from answering this post.

9 Likes

Next time, simply just add a repeat wait() until character. :rofl:

3 Likes