Team
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
คลาส Team เป็นตัวแทนกลุ่มในสถานที่ Roblox คลาสเดียวที่มีประสิทธิภาพสำหรับทีมคือในบริการ Teams เท่านั้น ทีมนำเสนอส่วนตัวที่มีประโยชน์สำหรับผู้พัฒนาที่ส
- คุณสมบัติที่ทำงาน 'ออกจากกล่อง'
- คุณสมบัติที่ผู้พัฒนาสามารถเขียนโปรแกรมเข้าสู่เกมของพวกเขา
พฤติกรรมทีมของตัวอย่าง ต่อไปนี้คือคุณสมบัติที่มีอยู่โดยปกติและไม่ต้องการให้ผู้พัฒนาโปรแกรมพฤติกรรมใด ๆ
- เมื่อเป็นส่วนหนึ่งของทีม, ชื่อข้างบนตัวละครของผู้เล่นจะถูกสีเป็น Model
- เมื่อใช้รายการผู้เล่นเริ่มต้นผู้ใช้จะถูกจัดเป็นกลุ่มและแสดงรวมกันเป็นทีม
- การตั้งค่า Player.Neutral ให้เป็นจริงจะทำให้ Player ไม่สามารถแยกออกจากทีมได้ แต่จะไม่เปลี่ยน Player.Team หรือ 1> Class.Player.TeamColor1>
- เมื่อ Player เข้าร่วมเกมพวกเขาจะได้รับการแจกจ่ายไปยังทีมด้วย Team.AutoAssignable ตั้งค่าให้เป็น true ที่มีผู้เล่นน้อยที่สุด หากไม่มีทีมที่สามารถแจกจ่ายได้
- เมื่อ SpawnLocation.Neutral ตั้งค่าเป็น false เท่านั้นผู้เล่นที่มี Player.TeamColor ตรงกับ SpawnLocation.TeamColor สามารถเรียกใช้ได้บน 11> Class.SpawnLocation1>
- เมื่อ SpawnLocation.AllowTeamChangeOnTouch ตั้งค่าเป็นจริงผู้เล่นจะเปลี่ยน Player.TeamColor ของพวกเขาเป็น SpawnLocation.TeamColor เมื่อพวกเขาสัมผัสกับ 1> Class.SpawnLocation1>
พฤติกรรมทีมขยายเวลาออปชัน รหัส
- ใส่รหัสตรวจสอบในโค้ดอาวุธเพื่อป้องกันไฟรีที่เป็นมิตร
- ใส่การตรวจสอบในประตูหรือคุณสมบัติอื่น ๆ ที่อนุญาตให้ใช้เฉพาะทีมบางทีมเท่านั้น
- ยอดคงเหลือ
ตัวอย่างโค้ด
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)
สรุป
คุณสมบัติ
สมบัตินี้กำหนดว่า Players จะถูกวางอัตโนมัติบน Team เมื่อเข้าร่วม หากทีมหลายทีมมีสมบัตินี้ตั้งค่าเป็น true ร็อบ็อกส์จะพยายามที่จะเหมือนทีมออกเม
สมบัตินี้ตั้งสีของ Team สีของสมาชิกทีมที่เป็นเจ้าของทีม สร้างสมาชิกทีมที่มีสีของผู้เล่นที่เป็นสมาชิกของทีม และสร้างสมาชิกที่มีสีของผู้เล่นที่อยู่เหนือหัวของผู้
วิธีการ
อีเวนต์
คุณสมบัติ
AutoAssignable
สมบัตินี้กำหนดว่า Players จะถูกวางอัตโนมัติบน Team เมื่อเข้าร่วม หากทีมหลายทีมมีสมบัตินี้ตั้งค่าเป็น true ร็อบ็อกส์จะพยายามที่จะเหมือนทีมออกเม
เมื่อ Player เข้าร่วมเกมพวกเขาจะได้รับการแจกไปที่ Team ด้วย Team.AutoAssignable ตั้งค่าเป็น 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)
TeamColor
สมบัตินี้ตั้งค่าสีของ Team สร้างค่า Player.TeamColor ของผู้เล่นที่เป็นสมาชิกของทีม
ความสามารถทีมต่างๆ ของ Roblox จำนวนมากขึ้นอยู่กับสีทีม ไม่ใช่ชื่อหรือวัตถุ เช่น SpawnLocations สามารถกำหนดได้ผ่าน SpawnLocation.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 ที่ได้รับการเกณฑ์สําหรับ Team รายการ Player จะถูกพิจารณาได้หากสมบัติของ 1>
ฟังก์ชันนี้มีตัวใช้งานที่เป็นไปได้หลายรายการรวมถึงการนับจำนวนผู้เล่นใน Team หรือให้ทุก Player ใน Team ด้วย 2>Class.Tool2>
ส่งค่ากลับ
ค่าย่อของ Class.Player|Players ใน Team
ตัวอย่างโค้ด
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 โดยตรง ฟังก์ชันใด ๆ ที่เกี่ยวข้องกับเหตุการณ์นี้จะถูกส่งผ่าน Player ตั
Team.PlayerAdded: Connect(function(ผู้เล่น) print(ผู้เล่น.Name.." ได้เข้าร่วมทีม") ปิด)
พารามิเตอร์
Class.Player ที่เพิ่มเข้ามา
ตัวอย่างโค้ด
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 กำลังออกจากเกม 1> Class.Player.Neutral1> ตั้งค่าเป็น true หรือ <
เหตุการณ์นี้เป็นเหตุการณ์ทีมและจะเพิ่มเมื่อ Player ออกจาก Team ที่เฉพาะ ใด ๆ การเชื่อมต่อกับเหตุการณ์นี้จะถูกส่งผ่าน Player ของผู้ออกจากทีม
Team.PlayerRemoved: Connect(function(ผู้เล่น) print(ผู้เล่น.Name.." ออกจากทีม") ปิด)
พารามิเตอร์
Class.Player ถูกลบออก
ตัวอย่างโค้ด
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)