Hey! I just created a script that gives red team players a certain shirt and pants, does it automatically add the shirt/pants for someone that joins later on?
Script:
wait(10)
for i, v in pairs(game.Teams.Red:GetPlayers()) do
local pantsidred = game.ReplicatedStorage.LockerRed.Pants.PantsTemplate
local shirtidred = game.ReplicatedStorage.LockerRed.Shirt.ShirtTemplate
local shirt = v.Character:FindFirstChild("Shirt")
local pants = v.Character:FindFirstChild("Pants")
print("test")
shirt.ShirtTemplate = shirtidred
pants.PantsTemplate = pantsidred
end
No, the script you provided will only affect players that are already in the game when it is run. It will not automatically give the shirt and pants to players who join the game later on.
Hereās a fix on this:
game.Players.PlayerAdded:Connect(function(player)
if player.Team.Name == āRedā then
local pantsidred = game.ReplicatedStorage.LockerRed.Pants.PantsTemplate
local shirtidred = game.ReplicatedStorage.LockerRed.Shirt.ShirtTemplate
local shirt = player.Character:FindFirstChild("Shirt")
local pants = player.Character:FindFirstChild("Pants")
shirt.ShirtTemplate = shirtidred
pants.PantsTemplate = pantsidred
end
i, v in pairs(game.Teams.Red:GetPlayers()) is not a loop, itās a more advanced way to iterate over a collection. In this case itās going to return two values for each member of the collection. The first value is the index of that item in the collection, and the second value is the player instance itself. for i, v in pairs(game.Teams.Red:GetPlayers()) do
This will iterate over all players in the Red team. local pantsidred = game.ReplicatedStorage.LockerRed.Pants.PantsTemplate
local shirtidred = game.ReplicatedStorage.LockerRed.Shirt.ShirtTemplate
This will get the pants and shirt templates from the LockerRed object in ReplicatedStorage. You can also use local pantsidred = game.ReplicatedStorage.LockerRed.Pants.PantsTemplate.Value if you have an actual ID saved in the āPantsTemplateā property. local shirt = v.Character:FindFirstChild(āShirtā)
local pants = v.Character:FindFirstChild(āPantsā)
This will get the shirt and pants for the player in the v variable. shirt.ShirtTemplate = shirtidred
pants.PantsTemplate = pantsidred
This will set the shirt and pants for the player in the v variable. end
This ends the for loop, and goes to the next player.
So I just add a while loop and check if they already have the shirt on and if they do, it will just return and if they do not, it will check if they have a shirt and it will change their shirt id?
You could repeatedly check if all players on the team have the shirt, but itās not really the best way. Instead, Iād suggest using event-based programming where you write code that reacts to certain things happening in the game instead of code that repeatedly manually checks if something has happened.
You can use the Team.PlayerAdded event to do something when a player gets added to a team, and the Player.CharacterAdded event to also do that thing when they (re)spawn. For example:
function giveUniform(player, uniform)
local character = player.Character
if not character then return end
local uniformPants = uniform:FindFirstChildWhichIsA("Pants")
character.Pants.PantsTemplate = uniformPants.PantsTemplate
local uniformShirt = uniform:FindFirstChildWhichIsA("Shirt")
character.Shirt.ShirtTemplate = uniformShirt.ShirtTemplate
end
game.Teams.Red.PlayerAdded:Connect(function(addedPlayer)
--Give the uniform when a player is first added to the team
giveUniform(addedPlayer, game.ReplicatedStorage.LockerRed)
--Connections, stored in variables so they can later be disconnected.
local spawnedC, removedC
--Also the uniform each time a player spawns if they're already on the team
spawnedC = addedPlayer.CharacterAdded:Connect(function()
giveUniform(addedPlayer, game.ReplicatedStorage.LockerRed)
end)
--Make sure to stop giving the uniform after the player has been removed from the.
removedC = game.Teams.Red.PlayerRemoved(removedPlayer)
if removedPlayer == addedPlayer then
spawnedC:Disconnect()
removedC:Disconnect()
end
end)
end)