Class.Team 代表 Roblox 场景置中的一个帮派。唯一有效的父级是在 Teams 服务中的团队。团队提供一系列有用于开发人员的功能,可以分为两个粗糙群组:
- 工作“方块子”的功能
- 允许开发人员编程他们的游戏。
内置团队行为 团队的以下功能默认存在,不需要开发者编程任何自定义行为。
- 当一个团队的一部分时,玩家角色上方的名称将被着色为 Model
- 使用默认玩家列表时,用户将被组成一个团队,并一起显示
- 当 Player 加入游戏时,他们将被分配到拥有最少玩家的团队,其中设置为 Team.AutoAssignable 为真实值,该为自动分配的团队不可用。如果没有可用的自动分配团队,Player.Neutral 将设置为真实值。
- 当 SpawnLocation.Neutral 设置为 false 时,只有玩家 whose Player.TeamColor 与 SpawnLocation.TeamColor 匹配的玩家才能在该 1> Class.SpawnLocation1> 上生成
- 当 SpawnLocation.AllowTeamChangeOnTouch 设置为 true 时,玩家的 Player.TeamColor 将更改为 SpawnLocation.TeamColor ,当他们的角色触摸到 1> Class.SpawnLocation1> 时。
可选扩展团队行为 许多开发人员选择将以下功能添加到自己的代验证码中的团队。
- 在武器代码中实现检查,以防止友军开触发。
- 在门或其他允许仅特定团队使用的功能上实现检查
- 定期重新分配团队以保持团队平余额
代码示例
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)
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])
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)
概要
属性
此属性确定 Players 将在加入时自动放置在 Team 上。如果多个团队设置了此属性为 true,Roblox 将尝试在团队出现时甚至尝试放置在 Players 上。
这个属性设置了Team的颜色。 确定玩家团队的成员的 Player.TeamColor 属性。 还决定显示在玩家列表上和玩家头上的颜色。
方法
活动
Class.Player.Team 的玩家是否被分配到 Team 。如果玩家的 Player.Team 属性与 1>Class.Team1> 和 4>Class.Player.Neutral4> 相等,则视为已分配。
当 Player 从 Team 中移除时,这可能是因为 Player 离开了游戏, 1>Class.Player.Neutral1> 设置为 true 或 4>Class.Player4> 加入另一个团队。
属性
AutoAssignable
此属性确定 Players 将在加入时自动放置在 Team 上。如果多个团队设置了此属性为 true,Roblox 将尝试在团队出现时甚至尝试放置在 Players 上。
当 Player 加入游戏时,他们将被分配到 Team 与 Team.AutoAssignable 设置为 true 有最少玩家。 如果没有此团队可用, 2>Class.Player.Neutral2> 将设置为 true。
注意使用此属性将有助于即使在玩家添加时仍然有效的团队,它不会在玩家被移除时做任何事情。因此,开发人员可能需要实现自己的团队平衡系统。
代码示例
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)
TeamColor
这个属性设置团队的颜色。 它是由 Team 玩家的 Class.Team 的颜色。
很多 Roblox 的默认团队功能基于团队颜色,而不是名称或对象。 例如,SpawnLocations 可以通过SpawnLocation.TeamColor分配到团队。 因此,建议开发人员确保每个Team都有独特的团队颜色。
任何参与团队的玩家都将其名称颜色改为团队的 TeamColor 属性。他们还将被列在玩家列表的团队头衔下。
代码示例
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)
方法
GetPlayers
返回一个列表Players 被分配到 Team 。一个 Player 被视为已分配,如果其 1> Class.Player.Team1> 属性与 4> Class.Team4> 和 7> Class.Player.Neutral7>
此函数有多个潜在使用例子,包括计数 Team 上的玩家数量或将每个 Player 上的 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
活动
PlayerAdded
Class.Player.Team 的玩家是否被分配到 Team 。如果玩家的 Player.Team 属性与 1>Class.Team1> 和 4>Class.Player.Neutral4> 相等,则视为已分配。
此事件是团队特定,仅发生在 Player 与特定 Team 共同。 任何与此事件相关的函数都会通过 Player 对玩家加入团队的对象传递。 例如:
Team.PlayerAdded:Connect(玩家unction(player) print(玩家layer.Name.." 已加入团队伍") 结束nd)
参数
代码示例
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)
PlayerRemoved
当 Player 从 Team 中移除时,这可能是因为 Player 离开了游戏, 1>Class.Player.Neutral1> 设置为 true 或 4>Class.Player4> 加入另一个团队。
此事件是团队特定,仅发生在 Player 离开具体的 Team 。 任何与此事件相关的函数都会通过玩家离开团队的 Player 对象传递。 例如:
团队.PlayerRemoved:Connect(玩家unction(playe玩家) print(player.Name.." 已离开团队") 结束nd)
参数
Class.Player 已移除。
代码示例
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)