팀 서비스는 게임의 Team 개체를 보유합니다. Team 개체는 팀 서비스에 부모가 되어야 합니다.
팀은 개발자에게 유용한 다양한 기능을 제공합니다.이들은 상자 밖으로 작동하는 기능과 개발자가 게임에 프로그래밍할 수 있는 기능으로 넓게 분할될 수 있습니다.
내장 팀 행동 팀의 다음 기능은 기본적으로 존재하며 개발자가 사용자 지정 행동을 프로그래밍할 필요가 없습니다.
- 팀의 일부일 때, 플레이어의 캐릭터 위에 있는 이름 Model 이 색상이 Team.TeamColor로 변경됩니다.
- 변경하는 는 해당하는 팀으로 전환을 유발합니다
- 기본 플레이어 목록을 사용할 때 사용자는 팀으로 그룹화되어 표시됩니다
- 게임에 Player 가 참여하면 플레이어가 가장 적은 팀에 Team.AutoAssignable 설정된 팀에 할당됩니다.자동으로 할당할 수 있는 팀이 없으면 Player.Neutral가 true로 설정됩니다
- 가 false로 설정되면, 와 일치하는 플레이어만 해당 에 생성될 수 있습니다.
- When 가 true로 설정되면 플레이어의 가 캐릭터가 을 만질 때 변경됩니다.
선택적으로 확장된 팀 행동 많은 개발자들이 자체 코드에서 팀에 다음 기능을 추가하기로 선택했습니다.
- 팀 살해를 방지하기 위해 무기 코드에서 팀에 대한 검사 구현
- 특정 팀만 사용할 수 있는 문이나 기타 기능 구현
- 정기적으로 팀을 재할당하여 팀 잔액유지
코드 샘플
This code sample includes a simple example of how to re-balance teams. When Team.AutoAssignable is set to true players will be added to Teams in a balanced fashion. However as Players leave the game this can lead to unbalanced teams as players are not reallocated. This code keeps track of the number of players in each team and, when players leave will check to see if the teams need re-balancing.
local Teams = game:GetService("Teams")
-- create two teams
local redTeam = Instance.new("Team")
redTeam.TeamColor = BrickColor.new("Bright red")
redTeam.AutoAssignable = true
redTeam.Name = "Red Team"
redTeam.Parent = Teams
local blueTeam = Instance.new("Team")
blueTeam.TeamColor = BrickColor.new("Bright blue")
blueTeam.AutoAssignable = true
blueTeam.Name = "Blue Team"
blueTeam.Parent = Teams
-- start counting the number of players on each team
local numberRed, numberBlue = 0, 0
local function playerAdded(team)
-- increase the team's count by 1
if team == redTeam then
numberRed = numberRed + 1
elseif team == blueTeam then
numberBlue = numberBlue + 1
end
end
local function playerRemoved(team)
-- decrease the team's count by 1
if team == redTeam then
numberRed = numberRed - 1
elseif team == blueTeam then
numberBlue = numberBlue - 1
end
-- check if the teams are unbalanced
local bigTeam, smallTeam = nil, nil
if (numberRed - numberBlue) > 2 then
bigTeam = redTeam
smallTeam = blueTeam
elseif (numberBlue - numberRed) > 2 then
bigTeam = blueTeam
smallTeam = redTeam
end
if bigTeam then
-- pick a random player
local playerList = bigTeam:GetPlayers()
local player = playerList[math.random(1, #playerList)]
-- check the player exists
if player then
-- change the player's team
player.TeamColor = smallTeam.TeamColor
-- respawn the player
player:LoadCharacter()
end
end
end
-- listen for players being added / removed
blueTeam.PlayerAdded:Connect(function(_player)
playerAdded(blueTeam)
end)
blueTeam.PlayerRemoved:Connect(function(_player)
playerRemoved(blueTeam)
end)
redTeam.PlayerAdded:Connect(function(_player)
playerAdded(redTeam)
end)
redTeam.PlayerRemoved:Connect(function(_player)
playerRemoved(redTeam)
end)
The following code sample will create a door in the Workspace that can only be walked through by Players on the Bright red team.
local Players = game:GetService("Players")
local door = Instance.new("Part")
door.Anchored = true
door.Size = Vector3.new(7, 10, 1)
door.Position = Vector3.new(0, 5, 0)
door.Parent = workspace
local debounce = false
door.Touched:Connect(function(hit)
if not debounce then
debounce = true
if hit then
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player and player.TeamColor == BrickColor.new("Bright red") then
door.Transparency = 0.5
door.CanCollide = false
task.wait(3)
door.Transparency = 0
door.CanCollide = true
end
end
task.wait(0.5)
debounce = false
end
end)
This code sample includes a quick function that can be added to weapons in a place to prevent them from team killing. It will return false when the two players are on different teams or if either of them is neutral.
local Players = game:GetService("Players")
function checkTeamKill(playerAttack, playerVictim)
if playerAttack.Team ~= playerVictim.Team or playerAttack.Neutral or playerVictim.Neutral then
return false
end
return true
end
local players = Players:GetPlayers()
checkTeamKill(players[1], players[2])
요약
메서드
게임의 개체가 포함된 테이블을 반환합니다. 서비스에 부모로 지정된 개체만 반환합니다.
속성
메서드
GetTeams
GetTeam 함수는 게임의 Team 개체가 포함된 테이블을 반환합니다.
이 경우 반환되는 팀 개체는 직접 Teams 서비스에 부모로 지정된 개체만 됩니다.이러한 이유로 개발자는 서비스에만 개체를 부모로 지정하고 다른 (또는 서로)에는 지정하지 않는 것이 좋습니다.
반환
게임에서의 Teams 배열.
코드 샘플
The example below prints the number of players on each Team.
local Teams = game:GetService("Teams")
local teams = Teams:GetTeams()
for _, team in pairs(teams) do
local players = team:GetPlayers()
print("Team", team.Name, "has", #players, "players")
end