How do i make it so a script executes only when a player is in a specific team?
COMMUNITY QUESTION
How do i make it so a script executes only when a player is in a specific team?
2 answers
Replace "Red" with whatever team you're looking for on the "game:GetService("Teams").Red" line
if(player.Team == game:GetService("Teams").Red) then
---code
end
You have to first set the players in a specific team so to do this, you can create a StringValue in the players when they join the game. This will act as a tag to determine which team they're on in the game. You can then loop through the players with a for loop and check if they have the tag for the specific team. If they do, then run the code for said team.
game.Players.PlayerAdded:Connect(function(players)
if players.Name == 'Boi' then
local redTeam = Instance.new('StringValue')
redTeam.Name = 'RedTeam'
redTeam.Parent = players
end
if players.Name == 'YoBoi1337' then
local blueTeam = Instance.new('StringValue')
blueTeam.Name = 'BlueTeam'
blueTeam.Parent = players
end
for i,v in pairs(players:GetChildren()) do
if v.Name == 'RedTeam' then
print('Player is on red team') -- execute the script you want on the red team
elseif v.Name == 'BlueTeam' then
print('Player is on blue team') -- execute the script you want on the blue team
end
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
If you want the teams to show up on the top right corner like a leaderboard, check out this article https://developer.roblox.com/en-us/articles/Leaderboards. It's very similar to the code i just showed you.