ラウンドの間、スクリプトは時間を追跡し、異なるスクリプト間でシグナルを送信する必要があります。時間は時間スクリプトを使用して管理され、イベント、Roblox のコーディングでのコンセプトは、一致合の終了などの変更をシグナルします。
イベントでシグナルを送信する
今、アリーナにプレイヤーがいるので、イベントは試合の開始をシグナルするために使用でき、タイマーのコードが開始できます。後で、イベントは、一致合の終了を示し、プレイヤーをロビーに戻す時間であることをシグナルすることもできます。
これらのイベントはプリビルドされていないので、 バインド可能なイベント と呼ばれるカスタムイベントオブジェクトを作成する必要があります。バインド可能イベントは、プレイヤーが発動するアクションによく使用され、Touched または Changed のようなイベントに似ています。
複数のスクリプトが同じバインド可能なイベントを聞くことができます。これにより、コードが整理され、後で必要に応じてマッチの開始または終了に追加コードを追加することが簡単になります。
バインド可能なイベントを作成する
マッチの開始と終了のバインド可能なイベントオブジェクトを作成して開始する。バインド可能なイベントは、クライアントと対話しないので、サーバーストレージに保存できます。
ServerStorage で、イベントという新しいフォルダを作成します。そのフォルダで、マッチ開始とマッチ終了という名前の バインド可能なイベント を 2 つ作成します。
イベントを使用
現在、プレイヤーがアリーナに入ると、インターミッションはタイマーを開始するのではなく、再起動し続けます。メインのゲームループは、コードの次の部分に移る前に、マッチエンドイベントが発動するまで停止して待つ必要があります。
イベントには 2つの内蔵機能があります: Connect() と Wait() 。以前のように Connect() を使用するのではなく、MatchEnd に Wait() を呼び出して、MatchEnd が発行されるまでゲームマネージャースクリプトを一時停止します。この場合、待機機能はゲームマネージャーが試合が終了したというシグナルを受け取るまでコードを一時停止します。
In ゲームマネージャー , create variables for the Events フォルダと MatchEnd イベントを作成します。
-- モジュールスクリプトlocal moduleScripts = ServerStorage:WaitForChild("ModuleScripts")local matchManager = require(moduleScripts:WaitForChild("MatchManager"))local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))-- イベントlocal events = ServerStorage:WaitForChild("Events")local matchEnd = events:WaitForChild("MatchEnd")スクリプトが移動する前に、マッチ終了イベントが発火するのを待機します。 ループ で、 終了 で、タイプ: matchEnd.Event:Wait()
while true dorepeattask.wait(gameSettings.intermissionDuration)print("Restarting intermission")until #Players:GetPlayers() >= gameSettings.minimumPlayersprint("Intermission over")task.wait(gameSettings.transitionTime)matchManager.prepareGame()-- ゲームの長さを待つプレースホルダー。matchEnd.Event:Wait()endテスト ゲーム。プレイヤーがアリーナに入ったとき、インターミッションループ が 続かないことを確認します。スクリプトは現在、matchEnd シグナルが発射するのを待っています。
トラブルシュートのヒント
この時点では、コードが期待通りに機能しないので、以下のいずれかを試してください。
- matchEnd.Event:Wait() でドットまたはコロン演算子の使用を二度チェックします。
- MatchEnd がバインド可能なイベントであり、リモートイベントなどの別のタイプではないことを確認してください。
タイマーを使用
マッチの終了を引き起こす条件の 1つは、スクリプトを通じて処理されるタイマー切れです。
タイマーを設定する
ゲームにタイマーを追加するには、以下のステップでプレイスドモジュールスクリプトを使用してください。タイマーを開始および終了する機能や、残り時間の量を返す機能などが含まれています。
ServerStorage > ModuleScripts で、タイマーという名前の新しいモジュールスクリプトを作成します。
コードを以下のコードで置き換えます。
local Timer = {}Timer.__index = Timerfunction Timer.new()local self = setmetatable({}, Timer)self._finishedEvent = Instance.new("BindableEvent")self.finished = self._finishedEvent.Eventself._running = falseself._startTime = nilself._duration = nilreturn selfendfunction Timer:start(duration)if not self._running thentask.spawn(function()self._running = trueself._duration = durationself._startTime = tick()while self._running and tick() - self._startTime < duration dotask.wait()endlocal completed = self._runningself._running = falseself._startTime = nilself._duration = nilself._finishedEvent:Fire(completed)end)elsewarn("Warning: timer could not start again as it is already running.")endendfunction Timer:getTimeLeft()if self._running thenlocal now = tick()local timeLeft = self._startTime + self._duration - nowif timeLeft < 0 thentimeLeft = 0endreturn timeLeftelsewarn("Warning: could not get remaining time, timer is not running.")endendfunction Timer:isRunning()return self._runningendfunction Timer:stop()self._running = falseendreturn Timerマッチマネージャでは、ゲーム設定とタイマモジュールが必要です。
local MatchManager = {}-- サービスlocal ServerStorage = game:GetService("ServerStorage")-- モジュールスクリプトlocal moduleScripts = ServerStorage:WaitForChild("ModuleScripts")local playerManager = require(moduleScripts:WaitForChild("PlayerManager"))local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))local timer = require(moduleScripts:WaitForChild("Timer"))変数の下で、変数名が と同じの新しいタイマーオブジェクトを作成します。このオブジェクトは、タイマーを開始および停止する関数を呼び出すのに使用されます。
local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))local timer = require(moduleScripts:WaitForChild("Timer"))-- マッチ時間を追跡するために使用される新しいタイマーオブジェクトを作成します。local myTimer = timer.new()
開始と停止
タイマーが作成されたので、マッチ中に含まれた機能 start() と stop() を使用します。以下は、それぞれの機能と受け入れるパラメータの説明です。
- start(time) - タイマーを開始し、秒単位の時間をパラメータとします。
- finished:Connect(functionName) - タイマーが終了すると、パラメータとして渡された関数を実行します。
In マッチマネージャー , create a new function named timeUp() をタイマーが終了したときに実行する新しい機能を作成します。テスト印刷文を含めます。
local myTimer = timer.new()-- ローカル機能local function timeUp()print("Time is up!")end-- モジュール機能function MatchManager.prepareGame()playerManager.sendPlayersToMatch()endreturn MatchManager以下の timeUp() 、print 文を使用して名前の startTimer() の機能を追加します。後でゲーム内でタイマーを表示します。
-- ローカル機能local function timeUp()print("Time is up!")endlocal function startTimer()print("Timer started")endタイマーを開始および停止するには、in startTimer() :
- 呼び出し myTimer.start() 。 gameSettings.matchDuration をパス。
- 呼び出し myTimer.finished:Connect() 。 timeUp() をパス。
-- ローカル機能local function startTimer()print("Timer started")myTimer:start(gameSettings.matchDuration)myTimer.finished:Connect(timeUp)end
タイマーを開始
タイマーは、マッチ開始イベントを使用して、試合開始時にトリガーできます。
マッチマネージャーでは、モジュール変数の下で、イベントフォルダ、マッチスタート、マッチエンド(将来のレッスンで使用される)を保存する変数を作成します。
-- モジュールスクリプトlocal moduleScripts = ServerStorage:WaitForChild("ModuleScripts")local playerManager = require(moduleScripts:WaitForChild("PlayerManager"))local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))local timer = require(moduleScripts:WaitForChild("Timer"))-- イベントlocal events = ServerStorage:WaitForChild("Events")local matchStart = events:WaitForChild("MatchStart")local matchEnd = events:WaitForChild("MatchEnd")--タイマーを作成するlocal myTimer = timer.new()上 return MatchManager 、マッチ開始イベントを startTimer() に接続します。
-- モジュール機能function MatchManager.prepareGame()playerManager.sendPlayersToMatch()endmatchStart.Event:Connect(startTimer)return MatchManagerマッチ開始イベントを発射するには、prepareGame() で matchStart:Fire() をタイプします。
-- モジュール機能function MatchManager.prepareGame()playerManager.sendPlayersToMatch()matchStart:Fire()endゲームをテストします。出力ウィンドウで、タイマーの開始と停止機能の印刷文を見ることができるかどうかを確認します。
完了したスクリプト
以下は、作業を確認するための完了したスクリプトです。
MatchManager スクリプト
local MatchManager = {}
-- サービス
local ServerStorage = game:GetService("ServerStorage")
-- モジュールスクリプト
local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
local playerManager = require(moduleScripts:WaitForChild("PlayerManager"))
local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
local timer = require(moduleScripts:WaitForChild("Timer"))
-- イベント
local events = ServerStorage:WaitForChild("Events")
local matchStart = events:WaitForChild("MatchStart")
local matchEnd = events:WaitForChild("MatchEnd")
-- マッチ時間を追跡するために使用される新しいタイマーオブジェクトを作成します。
local myTimer = timer.new()
-- ローカル機能
local function timeUp()
print("Time is up!")
end
local function startTimer()
print("Timer started")
myTimer:start(gameSettings.matchDuration)
myTimer.finished:Connect(timeUp)
end
-- モジュール機能
function MatchManager.prepareGame()
playerManager.sendPlayersToMatch()
matchStart:Fire()
end
matchStart.Event:Connect(startTimer)
return MatchManager
ゲームマネージャースクリプト
-- サービス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"))-- イベントlocal events = ServerStorage:WaitForChild("Events")local matchEnd = events:WaitForChild("MatchEnd")while true dorepeattask.wait(gameSettings.intermissionDuration)print("Restarting intermission")until #Players:GetPlayers() >= gameSettings.minimumPlayersprint("Intermission over")task.wait(gameSettings.transitionTime)matchManager.prepareGame()-- ゲームの長さを待つプレースホルダー。matchEnd.Event:Wait()end