Roblox · Сбор-а-тон · урок 5

Телепорты

Карта большая — добавим быстрые перемещения: список мест с картинками, клик переносит туда.

Структура

Синим — что добавляем на этом уроке.

Workspace
└─ Teleports (Folder)
   └─ Лес (Part) — площадка, имя = название места
      └─ Decal (Decal) — картинка места
StarterGui
└─ ListGui (ScreenGui)
   ├─ TeleportButton (TextButton) — «Карта»
   ├─ TeleportFrame (Frame) — Visible выкл.
   │  └─ ScrollingList (ScrollingFrame) — UIGridLayout
   │     └─ Sample (Frame) — шаблон: LocationImage (ImageButton), LocationName
   └─ TeleportLocal (LocalScript)

Шаги

  1. Workspace → Folder «Teleports»: в каждой зоне площадка (Anchored), имя — название места. В неё Decal с картинкой.
  2. В ListGui — кнопка «TeleportButton» и «TeleportFrame» с ScrollingList и шаблоном Sample: ImageButton «LocationImage», TextLabel «LocationName».
  3. LocalScript «TeleportLocal» в ListGui. Персонажа игрок переносит сам: он управляет своим персонажем, и сервер увидит перенос.
  4. PivotTo переносит модель целиком. В методичке менялся Position у PrimaryPart — так персонаж разваливается.

Скрипты

TeleportLocal

Где: StarterGui → ListGui · LocalScript
Зачем: Список мест и перенос.

-- игроки
local Players = game:GetService("Players")
-- этот игрок
local player = Players.LocalPlayer
-- точки телепорта
local teleports = workspace:WaitForChild("Teleports")
-- ListGui
local gui = script.Parent
-- кнопка карты
local button = gui:WaitForChild("TeleportButton")
-- список мест
local frame = gui:WaitForChild("TeleportFrame")
-- прокрутка
local scrolling = frame:WaitForChild("ScrollingList")
-- шаблон
local sample = scrolling:WaitForChild("Sample")

-- кнопка
button.MouseButton1Click:Connect(function()
    -- открыть / закрыть
    frame.Visible = not frame.Visible
-- конец функции
end)

-- каждое место
for _, point in ipairs(teleports:GetChildren()) do
    -- копия шаблона
    local card = sample:Clone()
    -- имя
    card.Name = point.Name
    -- подпись
    card.LocationName.Text = point.Name
    -- картинка места
    local decal = point:FindFirstChildOfClass("Decal")
    -- на кнопку
    card.LocationImage.Image = decal and decal.Texture or ""
    -- фон — цвет площадки
    card.LocationImage.BackgroundColor3 = point.Color
    -- показываем
    card.Visible = true
    -- в список
    card.Parent = scrolling
    -- клик по месту
    card.LocationImage.MouseButton1Click:Connect(function()
        -- персонаж
        local character = player.Character
        -- он есть
        if character and character:FindFirstChild("HumanoidRootPart") then
            -- переносим целиком над площадкой
            character:PivotTo(point.CFrame + Vector3.new(0, 4, 0))
            -- закрываем список
            frame.Visible = false
        -- конец проверки
        end
    -- конец функции
    end)
-- конец перебора
end

Проверка

Запусти Play (или Run) и убедись:

Roblox · Collect-a-thon · lesson 5

Teleports

The map is big — let's add fast travel: a list of places with pictures; a click takes you there.

Structure

Blue — what we add in this lesson.

Workspace
└─ Teleports (Folder)
   └─ Лес (Part) — a pad, name = the place name
      └─ Decal (Decal) — the place picture
StarterGui
└─ ListGui (ScreenGui)
   ├─ TeleportButton (TextButton) — "Карта" (map)
   ├─ TeleportFrame (Frame) — Visible off
   │  └─ ScrollingList (ScrollingFrame) — UIGridLayout
   │     └─ Sample (Frame) — template: LocationImage (ImageButton), LocationName
   └─ TeleportLocal (LocalScript)

Steps

  1. Workspace → Folder "Teleports": a pad in each zone (Anchored), named after the place, with a Decal picture on it.
  2. In ListGui put a button "TeleportButton" and a "TeleportFrame" with a ScrollingList and a Sample template: an ImageButton "LocationImage", a TextLabel "LocationName".
  3. LocalScript "TeleportLocal" in ListGui. The player moves their own character: they control it, and the server sees the move.
  4. PivotTo moves the whole model. The workbook changed the PrimaryPart's Position — that makes the character fall apart.

Scripts

TeleportLocal

Where: StarterGui → ListGui · LocalScript
Why: The list of places and travel.

-- players
local Players = game:GetService("Players")
-- this player
local player = Players.LocalPlayer
-- teleport points
local teleports = workspace:WaitForChild("Teleports")
-- ListGui
local gui = script.Parent
-- the map button
local button = gui:WaitForChild("TeleportButton")
-- the list of places
local frame = gui:WaitForChild("TeleportFrame")
-- scrolling
local scrolling = frame:WaitForChild("ScrollingList")
-- the template
local sample = scrolling:WaitForChild("Sample")

-- a button
button.MouseButton1Click:Connect(function()
    -- open / close
    frame.Visible = not frame.Visible
-- end of function
end)

-- each place
for _, point in ipairs(teleports:GetChildren()) do
    -- a copy of the template
    local card = sample:Clone()
    -- name
    card.Name = point.Name
    -- the caption
    card.LocationName.Text = point.Name
    -- the place picture
    local decal = point:FindFirstChildOfClass("Decal")
    -- onto the button
    card.LocationImage.Image = decal and decal.Texture or ""
    -- background — the pad's color
    card.LocationImage.BackgroundColor3 = point.Color
    -- show it
    card.Visible = true
    -- into the list
    card.Parent = scrolling
    -- a click on a place
    card.LocationImage.MouseButton1Click:Connect(function()
        -- the character
        local character = player.Character
        -- it exists
        if character and character:FindFirstChild("HumanoidRootPart") then
            -- move the whole character above the pad
            character:PivotTo(point.CFrame + Vector3.new(0, 4, 0))
            -- close the list
            frame.Visible = false
        -- end of check
        end
    -- end of function
    end)
-- end of loop
end

Testing

Press Play (or Run) and make sure: