จัดการผู้เล่น

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ตรงกันถึงเวลาเริ่มเพิ่มคุณสมบัติลงในมันในระหว่างการแข่งขันผู้เล่นสามารถมาและไปได้เนื่องจากเหตุนี้จึงจำเป็นต้องใช้โค้ดสําหรับภารกิจเช่นส่งผู้เล่นเข้าสู่การแข่งขันและติดตามผู้เล่นที่ใช้งานอยู่เพื่อจัดการภารกิจเหล่านี้ สร้างสคริปต์โมดูลชื่อ PlayerManager สคริปต์นี้จะเริ่มฟังก์ชันเพื่อส่งผู้เล่นเข้าสู่สนามด้วยอาวุธและจะขยายในภายหลังในซีรีส์

ตั้งค่าสคริปต์

เนื่องจากผู้จัดการผู้เล่นรวมฟังก์ชันที่ใช้โดยสคริปต์อื่น ๆ ไว้ด้วย มันจะเป็นสคริปต์โมดูล

  1. ใน ServerStorage > ModuleScripts เพิ่มสคริปต์โมดูลใหม่ชื่อ PlayerManagerจากนั้นเปลี่ยนชื่อตารางโมดูลให้ตรงกับชื่อสคริปต์และเพิ่มความคิดเห็นสำหรับฟังก์ชันท้องถิ่นและโมดูล


    local PlayerManager = {}
    -- ฟังก์ชันท้องถิ่น
    -- ฟังก์ชันของโมดูล
    return PlayerManager
  2. กำลังติดตาม:

    บริการ:

    • ผู้เล่น - รู้ว่าผู้เล่นได้เข้าร่วมหรือออกจากเกมแล้ว
    • หน่วยเก็บของเซิร์ฟเวอร์ - ที่เก็บอาวุธสำหรับผู้เล่น

    แผนที่และตัวแปรผู้เล่น:

    • ล็อบบี้จุดเกิด, โฟลเดอร์อารีน่า, และโฟลเดอร์จุดเกิดอารีน่า - ใช้เพื่อเทเลพอร์ตผู้เล่นไปยังพื้นที่ต่างๆ
    • คลังของผู้เล่นที่ใช้งาน - ติดตามผู้เล่นที่อยู่ในเกมในปัจจุบัน

    local PlayerManager = {}
    -- บริการ
    local Players = game:GetService("Players")
    local ServerStorage = game:GetService("ServerStorage")
    -- ตัวแปรแผนที่
    local lobbySpawn = workspace.Lobby.StartSpawn
    local arenaMap = workspace.Arena
    local spawnLocations = arenaMap.SpawnLocations
    -- ตัวแปรผู้เล่น
    local activePlayers = {}
    -- ฟังก์ชันท้องถิ่น
    -- ฟังก์ชันของโมดูล
    return PlayerManager
  3. สร้างฟังก์ชันโมดูลชื่อ sendPlayersToMatch() พร้อมการพิมพ์ทดสอบภายใน


    -- ฟังก์ชันท้องถิ่น
    -- ฟังก์ชันของโมดูล
    function PlayerManager.sendPlayersToMatch()
    print("Sending players to match")
    end
    return PlayerManager

สร้างผู้เล่นในล็อบบี้

ตอนนี้มีหลายตำแหน่งที่เกิดขึ้นหมายความว่าผู้เล่นจะเกิดที่ตำแหน่งสุ่มเมื่อเข้าร่วมเกมเพื่อให้แน่ใจว่าผู้เล่นจะเกิดในล็อบบี้ เปลี่ยนคุณสมบัติของผู้เล่น RespawnLocation

  1. สร้างฟังก์ชันท้องถิ่นใหม่ชื่อ onPlayerJoin() ด้วยพารามิเตอร์ของ playerในฟังก์ชันนั้น ตั้งตำแหน่งการเกิดใหม่ของผู้เล่นเป็นตัวแปร spawn ในล็อบบี้ที่สร้างไว้ก่อนหน้านี้


    -- ฟังก์ชันท้องถิ่น
    local function onPlayerJoin(player)
    player.RespawnLocation = lobbySpawn
    end
  2. เพิ่มส่วนกิจกรรมใต้ฟังก์ชันโมดูลของคุณ จากนั้นเชื่อมต่อ onPlayerJoin() กับอีเวนต์ของบริการผู้เล่น PlayerAdded


    -- ฟังก์ชันของโมดูล
    function PlayerManager.sendPlayersToMatch()
    print("Sending players to match")
    end
    -- อีเวนต์
    Players.PlayerAdded:Connect(onPlayerJoin)

เชื่อมต่อและทดสอบ

ตอนนี้โมดูลสามารถเชื่อมต่อและทดสอบได้เมื่อสร้างผู้จัดการผู้เล่นแล้ว จำเป็นต้องใช้เพื่อให้รหัสในสคริปต์โมดูลสามารถรันและส่งผู้เล่นไปยังล็อบบี้ได้

  1. กลับไปที่ MatchManager กำลังติดตาม:

    • บริการที่เก็บของเซิร์ฟเวอร์ * ModuleScripts โฟลเดอร์ลูกของ ServerStorage
    • สคริปต์โมดูลผู้จัดการผู้เล่น โมดูลสคริปต์ลูกของ moduleScripts

    local MatchManager = {}
    -- บริการ
    local ServerStorage = game:GetService("ServerStorage")
    -- สคริปต์โมดูล
    local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
    local playerManager = require(moduleScripts:WaitForChild("PlayerManager"))
    function MatchManager.prepareGame()
    playerManager.sendPlayersToMatch()
    end
    return MatchManager
  2. ใช้เซิร์ฟเวอร์ท้องถิ่น เซิร์ฟเวอร์ ที่มีผู้เล่นอย่างน้อยส่วนหนึ่งเพื่อทดสอบ กำลังติดตาม:

    • ผู้เล่นทั้งหมดจะเกิดในล็อบบี้
    • คำสั่งพิมพ์จาก PlayerManager ปรากฏในหน้าต่างเอาต์พุต
  3. เมื่อเสร็จแล้ว คลิกทำความสะอาดเพื่อปิดเซิร์ฟเวอร์

เคล็ดลับการแก้ปัญหา

ในจุดนี้ส่วนหนึ่งของสคริปต์ไม่ทำงานตามที่ตั้งใจไว้ ลองหนึ่งในสิ่งต่อไปนี้

  • ตรวจสอบชื่อชิ้นส่วนเช่นอารีนาหรือตำแหน่งของล็อบบี้ > StartSpawn โดยเฉพาะอย่างยิ่งถ้าคุณตั้งชื่อให้แตกต่างจากที่สอนในบทเรียน
  • ตรวจสอบให้แน่ใจว่ามีการใช้โมดูลในสคริปต์แต่ละครั้งโดยใช้ฟังก์ชัน require() และสะกดถูกต้อง

ส่งผู้เล่นไปยังสนาม

ตอนนี้ผู้เล่นจะเกิดในล็อบบี้แล้วเทเลพอร์ตพวกเขาไปยังการแข่งขันเมื่อช่วงระยะหยุดนิ่งสิ้นสุดเปลี่ยน RespawnLocation ของผู้เล่นเป็นสถานที่เกิดในอารีนาโดยใช้ฟังก์ชันในวัตถุผู้เล่นที่เรียกว่า ReloadCharacter()

  1. ไปที่สคริปต์ PlayerManager ด้านล่าง onPlayerJoin() เพิ่มฟังก์ชันท้องถิ่นใหม่ชื่อ preparePlayer()รวมสองพารามิเตอร์: player และ whichSpawn สถานที่เกิดที่จะส่งพวกเขา


    local activePlayers = {}
    -- ฟังก์ชันท้องถิ่น
    local function onPlayerJoin(player)
    player.RespawnLocation = lobbySpawn
    end
    local function preparePlayer(player, whichSpawn)
    end
    -- ฟังก์ชันของโมดูล
    function PlayerManager.sendPlayersToMatch()
    print("Sending players to match")
    end
  2. ตั้งตำแหน่งการเกิดใหม่ของผู้เล่นเป็น whichSpawn


    local function preparePlayer(player, whichSpawn)
    player.RespawnLocation = whichSpawn
    end
  3. บังคับให้ตัวละครรีโหลดโดยใช้ LoadCharacter() และผู้เล่นจะเกิดใหม่โดยใช้ตำแหน่งที่กำหนดใหม่


    local function preparePlayer(player, whichSpawn)
    player.RespawnLocation = whichSpawn
    player:LoadCharacter()
    end

ส่งผู้เล่นไปยังจุดเกิด

ตรวจสอบให้แน่ใจว่าผู้เล่นแต่ละคนถูกเทเลพอร์ตไปยังตำแหน่งจุดเกิดที่แตกต่างกันในสนามรบโดยใช้ลูป for เพื่อจัดทำรายการผู้เล่นที่ใช้งานการใช้ลูป for ช่วยให้คุณสามารถผ่านทุกค่าในคอลเลกชันผู้เล่นได้ ทำให้สคริปต์สามารถปรับตัวให้เหมาะกับจํานวนผู้เล่นที่หลากหลายได้

  1. ในฟังก์ชัน sendPlayersToMatch() ใช้ตัวแปรเพื่อสร้าง阵列ของสถานที่เกิดทั้งหมดในสนามแข่งโดยรับลูกหลานของโฟลเดอร์ Arena > SpawnLocations


    --ฟังก์ชันของโมดูล
    function PlayerManager.sendPlayersToMatch()
    local arenaSpawns = spawnLocations:GetChildren()
    end
  2. เพิ่มลูป for ด้านล่างเพื่อรับคอลเลกชันของผู้เล่นทั้งหมดแล้วจากนั้นจะเรียกซ้ำผ่านแต่ละคน เพื่อรับผู้เล่นให้พิมพ์: Players:GetPlayers()


    function PlayerManager.sendPlayersToMatch()
    local arenaSpawns = spawnLocations:GetChildren()
    for playerKey, whichPlayer in Players:GetPlayers() do
    end
    end

ติดตามและสร้าง

เมื่อเกมทำงาน ต้องระบุผู้ใช้ที่กำลังเล่นเพื่อให้พวกเขาสามารถถูกสร้างขึ้นในอารีน่าได้ในตอนเริ่มต้นของรอบ, ผู้เล่นทุกคนจะถูกติดตามในคลังของผู้เล่นที่ใช้งานอยู่阵列จะถูกใช้สำหรับฟังก์ชันที่แตกต่างกัน เช่น การเทเลพอร์ตหรือการกำหนดอาวุธ เพื่อให้แน่ใจว่าผู้เล่นที่ยังอยู่ในล็อบบี้ในระหว่างรอบไม่ได้รับผลกระทบ

  1. ในลูป for ใช้ table.insert() โดยใช้พารามิเตอร์สองตัวสำหรับแอร์ราย์ activePlayers และผู้เล่นที่จะเพิ่ม


    function PlayerManager.sendPlayersToMatch()
    local arenaSpawns = spawnLocations:GetChildren()
    for playerKey, whichPlayer in Players:GetPlayers() do
    table.insert(activePlayers, whichPlayer)
    end
    end
  2. เพื่อรับตำแหน่งการเกิดจากสนามแข่ง สร้างตัวแปรชื่อ spawnLocation และตั้งค่าให้เป็นอันดับแรกในดัชนี **** ในตาราง arenaSpawns


    for playerKey, whichPlayer in Players:GetPlayers() do
    table.insert(activePlayers, whichPlayer)
    local spawnLocation = arenaSpawns[1]
    end
  3. โทร preparePlayer() และส่งใน whichPlayer และ spawnLocationจากนั้นเนื่องจากตำแหน่งการเกิดที่เกิดขึ้นถูกใช้แล้ว ลบ มันออกจากตารางเพื่อให้ผู้เล่นคนต่อไปได้รับการเกิดที่แตกต่าง


    for playerKey, whichPlayer in Players:GetPlayers() do
    table.insert(activePlayers, whichPlayer)
    local spawnLocation = table.remove(arenaSpawns, 1)
    preparePlayer(whichPlayer, spawnLocation)
    end
  4. ทดสอบบนเซิร์ฟเวอร์ท้องถิ่น สถานที่

เคล็ดลับการแก้ปัญหา

ในจุดนี้คุณไม่เห็นผลลัพธ์ที่ตั้งใจไว้ ลองหนึ่งในต่อไปนี้

  • ใน GetPlayers() , ตรวจสอบให้แน่ใจว่ามี สอง ปิดวงเล็บ เช่น Class.Players.GetPlayers(|Players:GetPlayers()) ในข้อความ
  • ตรวจสอบซีรีส์ของการเรียกฟังก์ชันในสคริปต์โมดูล ตัวอย่างเช่น matchManager.prepareGame() ควรโทร playerManager.sendPlayersToMatch()

ให้อาวุธแก่ผู้เล่น

เมื่อรอบเริ่มต้น ผู้เล่นแต่ละคนในสนามจะได้รับอาวุธที่จะใช้

เพิ่มเครื่องมือ

อาวุธของผู้เล่นจะเป็นเครื่องมือ ในขณะที่เครื่องมือใดๆ ใน Roblox สามารถใช้งานได้ เราได้ให้ตัวอย่างดาบเพื่อเริ่มต้น

  1. นำอาวุธจากกล่องเครื่องมือหรือสร้างของคุณเอง (ดู Tools )

  2. วางอาวุธลงใน ServerStorage หากคุณกําลังสร้างเครื่องมือของคุณเอง ตรวจสอบให้แน่ใจว่าเครื่องมือมีชื่อว่าอาวุธเนื่องจากจะถูกใช้ในสคริปต์ภายหลัง

ให้เครื่องมือแก่ผู้เล่น

ตอนนี้เครื่องมืออยู่ในที่เก็บแล้ว ทำงานบนสคริปต์เพื่อผ่าน阵列ผู้เล่นที่ใช้งานและให้เครื่องมือแก่ผู้ใช้แต่ละราย

  1. ใน PlayerManager เพิ่มตัวแปรชื่อ playerWeapon สำหรับอาวุธใน ServerStorage


    -- ตัวแปรแผนที่
    local lobbySpawn = workspace.Lobby.StartSpawn
    local arenaMap = workspace.Arena
    local spawnLocations = arenaMap.SpawnLocations
    -- ตัวแปรผู้เล่น
    local activePlayers = {}
    local playerWeapon = ServerStorage.Weapon
  2. ใน preparePlayer() ใส่โค้ดต่อไปนี้เพื่อรับตัวละครของผู้เล่น


    local function preparePlayer(player, whichSpawn)
    player.RespawnLocation = whichSpawn
    player:LoadCharacter()
    local character = player.Character or player.CharacterAdded:Wait()
    end
  3. สร้างตัวแปรใหม่ชื่อดาบและใช้ฟังก์ชัน Clone() เพื่อสร้างสำเนาของอาวุธใน ServerStorageจากนั้นให้ผู้ปกครองดาบกับตัวละครของผู้เล่น


    local function preparePlayer(player, whichSpawn)
    player.RespawnLocation = whichSpawn
    player:LoadCharacter()
    local character = player.Character or player.CharacterAdded:Wait()
    local sword = playerWeapon:Clone()
    sword.Parent = character
    end
  4. ทดสอบบนเซิร์ฟเวอร์ท้องถิ่น เพื่อยืนยันว่าผู้เล่นทุกคนจะได้รับเครื่องมือเมื่อส่งไปยังสนามแข่ง โปรดจำไว้ว่าหากคุณยังคงทดสอบต่อไป ช่วงพักจะยังคงรีสตาร์ทและผู้เล่นจะเกิดใหม่ทุกๆ สองสามวินาทีจะได้รับการแก้ไขในบทเรียนถัดไป

สคริปต์สําเร็จ

ด้านล่างเป็นสคริปต์ที่เสร็จสมบูรณ์เพื่อตรวจสอบงานของคุณอีกครั้ง

สคริปต์ GameManager


-- บริการ
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
-- สคริปต์โมดูล
local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
local matchManager = require(moduleScripts:WaitForChild("MatchManager"))
local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
while true do
repeat
task.wait(gameSettings.intermissionDuration)
print("Restarting intermission")
until #Players:GetPlayers() >= gameSettings.minimumPlayers
print("Intermission over")
task.wait(gameSettings.transitionTime)
matchManager.prepareGame()
end

สคริปต์ MatchManager


local MatchManager = {}
-- บริการ
local ServerStorage = game:GetService("ServerStorage")
-- สคริปต์โมดูล
local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
local playerManager = require(moduleScripts:WaitForChild("PlayerManager"))
function MatchManager.prepareGame()
playerManager.sendPlayersToMatch()
end
return MatchManager

สคริปต์โมดูลผู้เล่นจัดการ


local PlayerManager = {}
-- บริการ
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
-- ตัวแปรแผนที่
local lobbySpawn = workspace.Lobby.StartSpawn
local arenaMap = workspace.Arena
local spawnLocations = arenaMap.SpawnLocations
-- ตัวแปรผู้เล่น
local activePlayers = {}
local playerWeapon = ServerStorage.Weapon
-- ฟังก์ชันท้องถิ่น
local function onPlayerJoin(player)
player.RespawnLocation = lobbySpawn
end
local function preparePlayer(player, whichSpawn)
player.RespawnLocation = whichSpawn
player:LoadCharacter()
local character = player.Character or player.CharacterAdded:Wait()
local sword = playerWeapon:Clone()
sword.Parent = character
end
-- ฟังก์ชันของโมดูล
function PlayerManager.sendPlayersToMatch()
print("Sending players to match")
local arenaSpawns = spawnLocations:GetChildren()
for playerKey, whichPlayer in Players:GetPlayers() do
table.insert(activePlayers, whichPlayer)
local spawnLocation = table.remove(arenaSpawns, 1)
preparePlayer(whichPlayer, spawnLocation)
end
end
--อีเวนต์
Players.PlayerAdded:Connect(onPlayerJoin)
return PlayerManager