Class.Team 類表示 Roblox 空間置中的派系。Teams 服務中的唯一有效的父級是 Class.Teams 服務。Teams 提供一系列有用於開發人員的功能,可以分為兩個粗陋的群組:
- 不包含在方塊子中的功能
- 程式員可以在遊戲中程式化。
內置團隊行為 下列功能的團隊預設存在,並且不需要開發人員程式化任何自訂行為。
- 當團隊的一部分時,玩家的角色名稱上方的 Model 將會變色至 Team.TeamColor
- 使用預設玩家列表時,用戶將會被組成一個團隊,並且一起顯示
- 當 Player 加入遊戲時,他們將被分配到最少玩家的團隊,其中 Team.AutoAssignable 設置為 true 設為 true。如果沒有可用的自動分配團隊, Player.Neutral 將設置為 true。
- 當 SpawnLocation.Neutral 設為 false 時,只有玩家的 Player.TeamColor 與 SpawnLocation.TeamColor 一致的玩家才能在 1> Class.SpawnLocation1> 上生成
- 當 SpawnLocation.AllowTeamChangeOnTouch 設為真時,玩家的 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)
概要
屬性
方法
活動
屬性
AutoAssignable
此屬性決定 Players 將在加入時自動放置在 Team 上嗎。如果多個隊伍設置此屬性為 true,Roblox 將在加入時嘗試放置隊伍。
當 Player 加入遊戲時,他們將被分配到 Team 與 Team.AutoAssignable 設置為真,這有最少玩家。如果沒有此類團隊可用, 1> Class.Player.Neutral1> 將設置為真。
注意使用此屬性將有助於團隊,當玩家被添加時,它不會在玩家被移除時做任何事情。因此,開發人員可能會想要實現自己的團隊平衡系統。
範例程式碼
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的顏色。Player.TeamColor 玩家的屬性,是團隊的成員。
很多 Roblox 的預設團隊功能是基於團隊顏色,而不是名稱或對物件。 例如,SpawnLocations 可以通過SpawnLocation.TeamColor分配到團隊。 因此,建議開發人員確保每個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)
方法
GetPlayers
返回 Players 以分配到 Team 。一個 Player 被視為已分配,如果其 1> Class.Player.Team1> 屬性與 4> Class.Team4> 和 7> Class.Player.Neutral7>
此功能有多個可能使用的情況,包括計數 Team 上的玩家數量或給每個 Player 在 Team 上的 2>Class.Tool2> 。
返回
Class.Player|Players 在 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
發射 當 Player 被指派到 Team 時。玩家會被視為指派到 Player.Team 。如果他們的 2>Class.Player.Team2> 屬性與 5>Class.Team5> 和 8>Class.Player.Neutral8> 一致,則會檢查出。
這個事件是團隊特定的,只會在 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> 設為真或 4> Class.Player4> 加入不同的團隊。
這個事件是團隊特定,只會在 Player 離開特定 Team 時才會發生。任何與此事件連接的功能都會通過 Player 對玩家離開團隊的對象傳送。 例如:
Team.PlayerRemoved:Connect(玩家unction(player) print(玩家layer.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)