遊戲循環已經編寫完成,現在是時候開始將功能添加到它中了。在比相符期間,玩家可以來去自前往。因此,需要代碼來執行任務,例如將玩家送入比賽並跟蹤已啟用的玩家。要管理這些任務,創建名為 PlayerManager 的模組腳本。
這個腳本會啟動一個功能,將玩家送入競技場並使用武器,之後在系列中擴展。
設定腳指令碼
因為玩家管理器包含其他腳本使用的功能,所以它會是模組指令碼。
在伺服器儲存 > 模組腳本中,新增名為 PlayerManager 的新模組腳本。然後,將模組表重命名為符合腳本名稱並添加本地和模組功能的評論。
local PlayerManager = {}-- 本地功能-- 模組功能return PlayerManager添加本地變量以追蹤中列目的:
服務::
- 玩家 - 知道玩家是否加入或離開了遊戲。
- 伺服器儲存 - 玩家武器的儲存。
地圖和玩家變量::
- 大廳生成、競技場文件夾和競技場生成文件夾 - 用於將玩家傳送到不同區域。
- 一個活躍玩家的數組 - 追蹤目前在遊戲中的玩家。
local PlayerManager = {}-- 服務local Players = game:GetService("Players")local ServerStorage = game:GetService("ServerStorage")-- 地圖變量local lobbySpawn = workspace.Lobby.StartSpawnlocal arenaMap = workspace.Arenalocal spawnLocations = arenaMap.SpawnLocations-- 玩家變量local activePlayers = {}-- 本地功能-- 模組功能return PlayerManager創建一個名為 sendPlayersToMatch() 的模組功能,內部包含測試印刷。
-- 本地功能-- 模組功能function PlayerManager.sendPlayersToMatch()print("Sending players to match")endreturn PlayerManager
在大廳生成玩家
目前,有多個重生位置,這意味著玩家在加入遊戲時會在隨機位置重生。為了確保玩家在大廳生成,請變更玩家的 RespawnLocation 屬性。
創建一個新的本地函數名為 onPlayerJoin() ,參數為 player 。在該功能中,將玩家的重生位置設為早先創建的大廳重生變量。
-- 本地功能local function onPlayerJoin(player)player.RespawnLocation = lobbySpawnend在模組功能下添加事件部分。然後,連接 onPlayerJoin() 到玩家服務的 PlayerAdded 事件。
-- 模組功能function PlayerManager.sendPlayersToMatch()print("Sending players to match")end-- 事件Players.PlayerAdded:Connect(onPlayerJoin)
連接和測試
現在模組可以連接和測試。創建了 PlayerManager 後,需要它以便該模組腳本的代碼可以運行並將玩家傳送到大廳。
返回到 匹配管理器 並創建變量以追蹤中列內容:
- 伺服器儲存 服務。
- 模組腳本 夾,屬於伺服器儲存的子夾。
- 玩家管理器 模組腳指令碼,是模組腳本的子模組。
local MatchManager = {}-- 服務local ServerStorage = game:GetService("ServerStorage")-- 模組腳本local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")local playerManager = require(moduleScripts:WaitForChild("PlayerManager"))function MatchManager.prepareGame()playerManager.sendPlayersToMatch()endreturn MatchManager使用至少最小玩家的本地伺服器 測試 。確認您可以看到以追蹤中內容:
- 所有玩家都會在大廳生成。
- 來自 PlayerManager 的列印聲明出現在輸出窗口。
完成後,單擊「清理」以關閉伺服器。
排除故障提示
在這個時候,腳本的部分無法正常運作,請嘗試以下方法之一。
- 檢查競技場、大廳等零件的名稱,或大廳 > StartSpawn的位置,特別是如果你在課程中以不同於指示的方式命名它們。
- 請確認模組在每個腳本中需要使用 require() 函數,並正確輸入。
將玩家送到競技場
現在玩家會在大廳生成,完成中場休息後將他們傳送到比賽中。將玩家的 變更為在競技場使用玩家對象中的函數呼叫的生成位置。
前往 PlayerManager 指令碼本,下面 onPlayerJoin() ,添加一個新的本地函數名為 preparePlayer() 的新功能。包括兩個參數:player 和 whichSpawn ,發送到的生成位置。
local activePlayers = {}-- 本地功能local function onPlayerJoin(player)player.RespawnLocation = lobbySpawnendlocal function preparePlayer(player, whichSpawn)end-- 模組功能function PlayerManager.sendPlayersToMatch()print("Sending players to match")end將玩家的重生位置設為 whichSpawn。
local function preparePlayer(player, whichSpawn)player.RespawnLocation = whichSpawnend使用 LoadCharacter() 強制角色重新載入,玩家將使用新獲得的位置重生。
local function preparePlayer(player, whichSpawn)player.RespawnLocation = whichSpawnplayer:LoadCharacter()end
將玩家傳送到生成點
使用 for 循環來迭代通過活動玩家陣列的每個玩家,以確保每個玩家都被傳送到競技場中不同的生成位置。使用 for 循環允許您通過玩家陣列中的每一個值,讓腳本適應各種玩家數量。
在 sendPlayersToMatch() 函數中,使用變量來創建一個包含所有競技場生成位置的陣列,通過獲得競技場 > SpawnLocations 文件夾的子目錄來實現。
--模組功能function PlayerManager.sendPlayersToMatch()local arenaSpawns = spawnLocations:GetChildren()end在下方添加 for 循環以獲得所有玩家的陣列,然後逐個循環通過每個玩家。要獲得玩家,請輸入:Players:GetPlayers()。
function PlayerManager.sendPlayersToMatch()local arenaSpawns = spawnLocations:GetChildren()for playerKey, whichPlayer in Players:GetPlayers() doendend
追蹤和生成
當遊戲運行時,需要確定哪些用戶正在玩,以便他們可以在競技場中生成。在回合開始時,每位玩家都會被記錄在一個包含已啟用玩家的數組中。該陣列將用於不同的功能,例如傳送或分配武器,以確保在回合期間仍然在大廳的玩家不受影響。
在 for 循環中,使用 table.insert() , 使用兩個參數來添加 activePlayers 陣列和玩家。
function PlayerManager.sendPlayersToMatch()local arenaSpawns = spawnLocations:GetChildren()for playerKey, whichPlayer in Players:GetPlayers() dotable.insert(activePlayers, whichPlayer)endend若要從競技場獲得生成位置,請創建一個名為 spawnLocation 的變量,並將其設為 第一 索引在 arenaSpawns 表中。
for playerKey, whichPlayer in Players:GetPlayers() dotable.insert(activePlayers, whichPlayer)local spawnLocation = arenaSpawns[1]end呼叫 preparePlayer() 並傳送 whichPlayer 和 spawnLocation 。然後,因為那個重生位置已被使用, 移除 它從表中,以便下一位玩家獲得不同的生成。
for playerKey, whichPlayer in Players:GetPlayers() dotable.insert(activePlayers, whichPlayer)local spawnLocation = table.remove(arenaSpawns, 1)preparePlayer(whichPlayer, spawnLocation)end在本地 伺服器 上測試玩家被送到競技場。玩家將繼續在同一位置重生,因為將他們送回大廳的代碼尚未實空間。
排除故障提示
在這個時候,你沒有看到預期的結果,試試以下其中一個。
- 在 GetPlayers() 中,請確保有 兩個 關閉括號,例如 Class.Players.GetPlayers(|Players:GetPlayers()) 在文件中。
- 檢查模組腳本的功能呼叫序列。例如,matchManager.prepareGame()應該呼叫playerManager.sendPlayersToMatch()。
給玩家武器
當回合開始時,競技場的每一位玩家都會獲得一個武器來使用。
新增工具
玩家武器將是工具。雖然 Roblox 中的任何工具都可以使用,但我們已提供了一把示例劍作為啟動。
從工具箱中匯入武器,或創建自己的 (見 Tools )。
將武器放置在伺服器儲存中。如果你正在創建自己的工具,請確保工具名稱為武器,因為它將在稍後的腳本中使用。
向玩家提供工具
現在工具已存儲在儲存中,請寫一個腳本來通過活動玩家列表並為每個用戶提供該工具。
在 PlayerManager 中,添加名為 playerWeapon 的變量到伺服器儲存中的武器。
-- 地圖變量local lobbySpawn = workspace.Lobby.StartSpawnlocal arenaMap = workspace.Arenalocal spawnLocations = arenaMap.SpawnLocations-- 玩家變量local activePlayers = {}local playerWeapon = ServerStorage.Weapon在 preparePlayer() 中,貼上以下代碼以獲得玩家的角色。
local function preparePlayer(player, whichSpawn)player.RespawnLocation = whichSpawnplayer:LoadCharacter()local character = player.Character or player.CharacterAdded:Wait()end創建一個名為劍的新變量,並使用 Clone() 函數創建武器的副本在 ServerStorage 中。然後,將劍親切到玩家的角色。
local function preparePlayer(player, whichSpawn)player.RespawnLocation = whichSpawnplayer:LoadCharacter()local character = player.Character or player.CharacterAdded:Wait()local sword = playerWeapon:Clone()sword.Parent = characterend在本地伺服器上測試 以確認每個玩家在傳送到競技場時都會獲得工具。 請記住,如果您繼續測試,中場休息將一直重新啟動,因此玩家每幾秒鐘就會重生。這將在下一個課程中解決。
已完成的腳本
以下是完成的腳本來檢查您的工作。
遊戲管理員指令碼
-- 服務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 dorepeattask.wait(gameSettings.intermissionDuration)print("Restarting intermission")until #Players:GetPlayers() >= gameSettings.minimumPlayersprint("Intermission over")task.wait(gameSettings.transitionTime)matchManager.prepareGame()end
匹配管理員腳指令碼
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