Roblox · Захват флага · урок 2
Окно выбора команды: игрок жмёт «Синие» или «Красные», попадает в команду и переносится на её половину лобби.
Синим — что добавляем на этом уроке.
Teams ├─ Синие (Team) — TeamColor Really blue, AutoAssignable выкл. └─ Красные (Team) — TeamColor Really red, AutoAssignable выкл. Workspace ├─ Lobby (Model) │ └─ Neutral (SpawnLocation) — Neutral вкл. — до выбора команды ├─ LobbySpawn (Folder) │ └─ Синие, Красные (SpawnLocation) — Neutral выкл., TeamColor команды └─ GameSpawn (Folder) — на поле └─ Синие, Красные (SpawnLocation) — Neutral выкл., TeamColor команды ReplicatedStorage ├─ TeamEvent (RemoteEvent) └─ Lobby (BoolValue) — Value вкл. ServerScriptService └─ TeamsServer (Script) StarterGui └─ TeamGui (ScreenGui) — ResetOnSpawn выкл. ├─ Frame (Frame) │ └─ Синие, Красные (TextButton) — имя кнопки = имя команды └─ TeamsLocal (LocalScript)
Где: StarterGui → TeamGui · LocalScript
Зачем: Кнопки команд.
-- общее хранилище
local RS = game:GetService("ReplicatedStorage")
-- событие выбора команды
local teamEvent = RS:WaitForChild("TeamEvent")
-- TeamGui
local gui = script.Parent
-- окно с кнопками
local frame = gui:WaitForChild("Frame")
-- все объекты окна
for _, button in ipairs(frame:GetChildren()) do
-- только кнопки
if button:IsA("TextButton") then
-- нажатие
button.MouseButton1Click:Connect(function()
-- имя кнопки = имя команды
teamEvent:FireServer(button.Name)
-- прячем окно
gui.Enabled = false
-- конец функции
end)
-- конец проверки
end
-- конец перебора
endГде: ServerScriptService · Script
Зачем: Записывает в команду и переносит на её точку.
-- команды
local Teams = game:GetService("Teams")
-- общее хранилище
local RS = game:GetService("ReplicatedStorage")
-- событие выбора
local teamEvent = RS:WaitForChild("TeamEvent")
-- true — идёт лобби, false — игра
local lobby = RS:WaitForChild("Lobby")
-- игрок нажал кнопку
teamEvent.OnServerEvent:Connect(function(player, teamName)
-- от клиента пришло не имя — игнорируем
if typeof(teamName) ~= "string" then return end
-- команда с таким именем
local team = Teams:FindFirstChild(teamName)
-- нет такой или игрок уже в ней
if not team or not team:IsA("Team") or player.Team == team then return end
-- записываем в команду
player.Team = team
-- лобби или поле — смотря что идёт
local folder = lobby.Value and workspace.LobbySpawn or workspace.GameSpawn
-- точка появления команды
player.RespawnLocation = folder:FindFirstChild(team.Name)
-- возрождаем там
player:LoadCharacter()
-- конец функции
end)Запусти Play (или Run) и убедись:
Roblox · Capture the flag · lesson 2
A team selection window: the player presses "Синие" (Blue) or "Красные" (Red), joins the team and moves to its half of the lobby.
Blue — what we add in this lesson.
Teams ├─ Синие (Team) — TeamColor Really blue, AutoAssignable off └─ Красные (Team) — TeamColor Really red, AutoAssignable off Workspace ├─ Lobby (Model) │ └─ Neutral (SpawnLocation) — Neutral on — until a team is chosen ├─ LobbySpawn (Folder) │ └─ Синие, Красные (SpawnLocation) — Neutral off, the team's TeamColor └─ GameSpawn (Folder) — on the field └─ Синие, Красные (SpawnLocation) — Neutral off, the team's TeamColor ReplicatedStorage ├─ TeamEvent (RemoteEvent) └─ Lobby (BoolValue) — Value on ServerScriptService └─ TeamsServer (Script) StarterGui └─ TeamGui (ScreenGui) — ResetOnSpawn off ├─ Frame (Frame) │ └─ Синие, Красные (TextButton) — button name = team name └─ TeamsLocal (LocalScript)
Where: StarterGui → TeamGui · LocalScript
Why: Team buttons.
-- shared storage
local RS = game:GetService("ReplicatedStorage")
-- the team selection event
local teamEvent = RS:WaitForChild("TeamEvent")
-- TeamGui
local gui = script.Parent
-- the window with the buttons
local frame = gui:WaitForChild("Frame")
-- all objects in the window
for _, button in ipairs(frame:GetChildren()) do
-- buttons only
if button:IsA("TextButton") then
-- a click
button.MouseButton1Click:Connect(function()
-- button name = team name
teamEvent:FireServer(button.Name)
-- hide the window
gui.Enabled = false
-- end of function
end)
-- end of check
end
-- end of loop
endWhere: ServerScriptService · Script
Why: Puts the player into the team and moves them to its point.
-- teams
local Teams = game:GetService("Teams")
-- shared storage
local RS = game:GetService("ReplicatedStorage")
-- the selection event
local teamEvent = RS:WaitForChild("TeamEvent")
-- true — lobby time, false — the game
local lobby = RS:WaitForChild("Lobby")
-- the player pressed a button
teamEvent.OnServerEvent:Connect(function(player, teamName)
-- the client didn't send a name — ignore it
if typeof(teamName) ~= "string" then return end
-- the team with this name
local team = Teams:FindFirstChild(teamName)
-- no such team or the player is already in it
if not team or not team:IsA("Team") or player.Team == team then return end
-- put into the team
player.Team = team
-- lobby or field — depending on what's on
local folder = lobby.Value and workspace.LobbySpawn or workspace.GameSpawn
-- the team's spawn point
player.RespawnLocation = folder:FindFirstChild(team.Name)
-- respawn there
player:LoadCharacter()
-- end of function
end)Press Play (or Run) and make sure: