Team
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
คลาส Team แทนตัวแทนกลุ่มในสถานที่ Robloxพ่อที่ถูกต้องเพียงรายเดียวสำหรับทีมอยู่ในบริการ Teamsทีมมีคุณสมบัติหลากหลายที่มีประโยชน์สำหรับนักพัฒนาที่สามารถแบ่งออกเป็นกลุ่มหยาบสองกลุ่ม:
- คุณลักษณะที่ทำงาน 'ออกจากกล่อง'
- คุณลักษณะนักพัฒนาสามารถโปรแกรมลงในเกมของพวกเขา
พฤติกรรมทีมที่ติดตั้งไว้ ฟังก์ชันต่อไปนี้ของทีมมีอยู่โดยปกติและไม่จำเป็นต้องให้นักพัฒนาโปรแกรมพฤติกรรมที่กำหนดเองใดๆ
- เมื่อเป็นส่วนหนึ่งของทีม ชื่อบนตัวละครของผู้เล่น Model จะถูกสีเป็น Team.TeamColor
- เมื่อใช้รายชื่อผู้เล่นเริ่มต้นผู้ใช้จะถูกจัดกลุ่มและแสดงร่วมกันเป็นทีม
- การตั้งค่า Player.Neutral เป็นจริงจะทำให้ Player ถูกแยกออกจากทีม แต่จะไม่เปลี่ยน Player.Team หรือ Player.TeamColor
- เมื่อ Player เข้าร่วมเกม พวกเขาจะถูกจัดสรรให้กับทีมที่มี Team.AutoAssignable ตั้งค่าเป็นจริงที่มีผู้เล่นน้อยที่สุดหากไม่มีทีมที่สามารถกำหนดได้อัตโนมัติ Player.Neutral จะถูกตั้งค่าเป็นจริง
- เมื่อ SpawnLocation.Neutral ถูกตั้งค่าเป็น false เฉพาะผู้เล่นที่มี Player.TeamColor ตรงกับ SpawnLocation.TeamColor สามารถสร้างบนที่ SpawnLocation
- เมื่อ ถูกตั้งค่าเป็นจริง ตัวผู้เล่นจะเปลี่ยนเป็น เมื่อตัวละครของพวกเขาสัมผัสกับ
พฤติกรรมทีมขยายตัวเลือก นักพัฒนาจำนวนมากเลือกที่จะเพิ่มคุณลักษณะต่อไปนี้ให้กับทีมในโค้ดของตนเอง
- ดำเนินการตรวจสอบในรหัสอาวุธเพื่อป้องกันการยิงกันเอง
- ดำเนินการตรวจสอบในประตูหรือคุณลักษณะอื่นที่อนุญาตให้ทีมที่กำหนดเท่านั้นใช้งานได้
- เปลี่ยนทีมเป็นระยะเวลาเพื่อรักษาความสมดุลของทีม
ตัวอย่างโค้ด
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)
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])
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 demonstrates how to assign players to Teams based on their Player.PartyId using the SocialService:GetPlayersByPartyId() method. When a player joins the experience, the script checks whether they're part of a party. If so, it attempts to assign them to the same team as other party members. If no match is found or the player isn't in a party, they're placed on the team with fewer members to maintain balance.
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local SocialService = game:GetService("SocialService")
local redTeam = Instance.new("Team")
redTeam.Name = "Red Team"
redTeam.TeamColor = BrickColor.Red()
redTeam.AutoAssignable = false
redTeam.Parent = Teams
local blueTeam = Instance.new("Team")
blueTeam.Name = "Blue Team"
blueTeam.TeamColor = BrickColor.Blue()
blueTeam.AutoAssignable = false
blueTeam.Parent = Teams
local function assignPlayerToTeam(player)
local partyId = player.PartyId
if partyId == "" then
assignBalancedTeam(player)
return
end
local teams = { redTeam, blueTeam }
local matchingTeam = nil
local partyPlayers = SocialService:GetPlayersByPartyId(partyId)
for _, partyPlayer in partyPlayers do
if partyPlayer ~= player and table.find(teams, partyPlayer.Team) then
matchingTeam = partyPlayer.Team
break
end
end
if matchingTeam then
player.Team = matchingTeam
player.TeamColor = matchingTeam.TeamColor
else
assignBalancedTeam(player)
end
end
function assignBalancedTeam(player)
local redCount = #redTeam:GetPlayers()
local blueCount = #blueTeam:GetPlayers()
local chosenTeam = redCount <= blueCount and redTeam or blueTeam
player.Team = chosenTeam
player.TeamColor = chosenTeam.TeamColor
end
local function groupPlayersByParty()
local seenPartyIds = {}
local groupedPlayers = {}
for _, player in Players:GetPlayers() do
if player.PartyId == "" then
table.insert(groupedPlayers, { player })
elseif not table.find(seenPartyIds, player.PartyId) then
table.insert(seenPartyIds, player.PartyId)
local partyPlayers = SocialService:GetPlayersByPartyId(player.PartyId)
table.insert(groupedPlayers, partyPlayers)
end
end
return groupedPlayers
end
-- Call this function to rebalance teams
local function rebalanceTeams()
local groups = groupPlayersByParty()
table.sort(groups, function(a, b)
return #a > #b
end)
for _, player in Players:GetPlayers() do
player.Team = nil
end
for _, group in groups do
local redCount = #redTeam:GetPlayers()
local blueCount = #blueTeam:GetPlayers()
local bestTeam
if redCount <= blueCount then
bestTeam = redTeam
else
bestTeam = blueTeam
end
for _, player in group do
player.Team = bestTeam
player.TeamColor = bestTeam.TeamColor
end
end
for _, player in Players:GetPlayers() do
player:LoadCharacter()
end
end
Players.PlayerAdded:Connect(function(player)
assignPlayerToTeam(player)
player:LoadCharacter()
player:GetPropertyChangedSignal("PartyId"):Connect(function()
if player.PartyId ~= "" then
assignPlayerToTeam(player)
end
end)
end)
สรุป
คุณสมบัติ
คุณสมบัตินี้กำหนดว่า Players จะถูกวางอัตโนมัติบน Team เมื่อเข้าร่วมหากทีมหลายทีมมีค่านี้ตั้งเป็นจริง Roblox จะพยายามทำให้ทีมเท่ากันเมื่อ Players ถูกเพิ่มเข้ามา
คุณสมบัตินี้ตั้งสีของ Team ไว้กำหนดคุณสมบัติ Player.TeamColor ของผู้เล่นที่เป็นสมาชิกของทีมยังกำหนดสีที่แสดงบนรายการผู้เล่นและเหนือหัวผู้เล่น
วิธีการ
ส่งคืนรายการของ Players ที่ได้รับมอบหมายให้กับ TeamA Player ถือว่าได้รับการกำหนดหากคุณสมบัติ Player.Team ของพวกเขาเท่ากับ Team และ Player.Neutral เป็นเท็จ
อีเวนต์
ไฟจะปะทุเมื่อใดก็ตามที่ Player ถูกกำหนดให้กับ Teamผู้เล่นจะถูกจัดสรรถ้าคุณสมบัติ Player.Team ของพวกเขาเท่ากับ Team และ Player.Neutral เป็นเท็จ
ไฟจะปะทุเมื่อใดก็ตามที่ Player ถูกลบออกจาก Teamสาเหตุนี้อาจเป็นเพราะ Player ออกจากเกม Player.Neutral ถูกตั้งค่าเป็นจริงหรือ Player เข้าร่วมทีมที่แตกต่าง
คุณสมบัติ
AutoAssignable
คุณสมบัตินี้กำหนดว่า Players จะถูกวางอัตโนมัติบน Team เมื่อเข้าร่วมหากทีมหลายทีมมีค่านี้ตั้งเป็นจริง Roblox จะพยายามทำให้ทีมเท่ากันเมื่อ Players ถูกเพิ่มเข้ามา
เมื่อ Player เข้าร่วมเกมพวกเขาจะได้รับการจัดสรรให้กับ Team ด้วย Team.AutoAssignable ตั้งค่าเป็นจริงที่มีผู้เล่นน้อยที่สุดหากไม่มีทีมดังกล่าวที่พร้อมใช้งาน Player.Neutral จะถูกตั้งเป็นจริง
หมายเหตุในขณะที่ใช้คุณสมบัตินี้จะช่วยให้ทีมสมดุลเมื่อผู้เล่นถูกเพิ่ม มันจะไม่ทำอะไรเมื่อผู้เล่นถูกลบออกด้วยเหตุผลนี้ผู้พัฒนาอาจต้องการที่จะใช้ระบบการจัดสมดุลทีมของตนเอง
ตัวอย่างโค้ด
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)
TeamColor
คุณสมบัตินี้กำหนดสีของ Team กำหนดคุณสมบัติ Player.TeamColor ของผู้เล่นที่เป็นสมาชิกของทีม
ฟังก์ชันทีมเริ่มต้นมากมายของ Roblox ขึ้นอยู่กับสีทีมมากกว่าชื่อหรือวัตถุตัวอย่างเช่น SpawnLocations สามารถกำหนดให้กับทีมผ่าน SpawnLocation.TeamColor ได้ด้วยเหตุผลนี้จึงแนะนำให้นักพัฒนาตรวจสอบให้แน่ใจว่าแต่ละ Team มีสีทีมที่ไม่ซ้ำกัน
ผู้เล่นใดก็ตามที่เป็นส่วนหนึ่งของทีมจะมีชื่อเปลี่ยนเป็นค่าชื่อทีมของทีมในคุณสมบัติ TeamColor ของทีมพวกเขาจะถูกวางไว้ใต้หัวข้อทีมบนรายชื่อผู้เล่นด้วย
ตัวอย่างโค้ด
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)
วิธีการ
GetPlayers
ส่งคืนรายการของ Players ที่ได้รับมอบหมายให้กับ TeamA Player ถือว่าได้รับการกำหนดหากคุณสมบัติ Player.Team ของพวกเขาเท่ากับ Team และ Player.Neutral เป็นเท็จ
ฟังก์ชันนี้มีจำนวนการใช้งานที่อาจเกิดขึ้นหลายรายการ, รวมถึงการนับจํานวนผู้เล่นบน Team หรือให้ทุก Player บน Team บน Tool
ส่งค่ากลับ
ตัวอย่างโค้ด
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
อีเวนต์
PlayerAdded
ไฟจะปะทุเมื่อใดก็ตามที่ Player ถูกกำหนดให้กับ Teamผู้เล่นจะถูกจัดสรรถ้าคุณสมบัติ Player.Team ของพวกเขาเท่ากับ Team และ Player.Neutral เป็นเท็จ
อีเวนต์นี้เป็นเฉพาะทีมและจะยิงเฉพาะเมื่อมีการเชื่อมต่อ Player ร่วมกันเฉพาะ Teamฟังก์ชันใดๆ ที่เชื่อมโยงกับเหตุการณ์นี้จะได้รับวัตถุ Player ของผู้เล่นที่เข้าร่วมทีมตัวอย่าง:
Team.PlayerAdded:Connect(ฟังก์ชัน (ผู้เล่น) พิมพ์ชื่อผู้เล่น.." ได้เข้าร่วมทีม) จบ
พารามิเตอร์
ตัวอย่างโค้ด
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)
PlayerRemoved
ไฟจะปะทุเมื่อใดก็ตามที่ Player ถูกลบออกจาก Teamสาเหตุนี้อาจเป็นเพราะ Player ออกจากเกม Player.Neutral ถูกตั้งค่าเป็นจริงหรือ Player เข้าร่วมทีมที่แตกต่าง
อีเวนต์นี้เป็นเฉพาะทีมและจะยิงเฉพาะเมื่อ Player ออกจาก Team ที่เฉพาะฟังก์ชันใดๆ ที่เชื่อมโยงกับเหตุการณ์นี้จะได้รับวัตถุ Player ของผู้เล่นที่ออกจากทีมตัวอย่าง:
Team.PlayerRemoved:Connect(ฟังก์ชัน (ผู้เล่น) พิมพ์ชื่อผู้เล่น.." ได้ออกจากทีมแล้ว) จบ
พารามิเตอร์
ตัวอย่างโค้ด
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)