Does i, v in pairs function as a loop?

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
1 Like

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

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?

9 Likes

Did you add it in the pairs loop?

Not yet, but if I add a while loop Iā€™ll add it

9 Likes

I think after the i,v loop ended, the while loop will not continue either, sorry if Iā€™m incorrect here since I donā€™t use pair loops that much.

Is there a way to loop it? (i tested myself and it wont loop either)

9 Likes

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)

If you really want to use a loop, yes you can just wrap the whole for loop in a while true do loop (with a wait call so it doesnā€™t crash).