タイマーとイベント

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

ラウンドの間、スクリプトは時間を追跡し、異なるスクリプト間でシグナルを送信する必要があります。時間は時間スクリプトを使用して管理され、イベント、Roblox のコーディングでのコンセプトは、一致合の終了などの変更をシグナルします。

イベントでシグナルを送信する

今、アリーナにプレイヤーがいるので、イベントは試合の開始をシグナルするために使用でき、タイマーのコードが開始できます。後で、イベントは、一致合の終了を示し、プレイヤーをロビーに戻す時間であることをシグナルすることもできます。

これらのイベントはプリビルドされていないので、 バインド可能なイベント と呼ばれるカスタムイベントオブジェクトを作成する必要があります。バインド可能イベントは、プレイヤーが発動するアクションによく使用され、Touched または Changed のようなイベントに似ています。

複数のスクリプトが同じバインド可能なイベントを聞くことができます。これにより、コードが整理され、後で必要に応じてマッチの開始または終了に追加コードを追加することが簡単になります。

バインド可能なイベントを作成する

マッチの開始と終了のバインド可能なイベントオブジェクトを作成して開始する。バインド可能なイベントは、クライアントと対話しないので、サーバーストレージに保存できます。

  1. ServerStorage で、イベントという新しいフォルダを作成します。そのフォルダで、マッチ開始とマッチ終了という名前の バインド可能なイベント を 2 つ作成します。

イベントを使用

現在、プレイヤーがアリーナに入ると、インターミッションはタイマーを開始するのではなく、再起動し続けます。メインのゲームループは、コードの次の部分に移る前に、マッチエンドイベントが発動するまで停止して待つ必要があります。

イベントには 2つの内蔵機能があります: Connect()Wait() 。以前のように Connect() を使用するのではなく、MatchEnd に Wait() を呼び出して、MatchEnd が発行されるまでゲームマネージャースクリプトを一時停止します。この場合、待機機能はゲームマネージャーが試合が終了したというシグナルを受け取るまでコードを一時停止します。

  1. 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")
  2. スクリプトが移動する前に、マッチ終了イベントが発火するのを待機します。 ループ で、 終了 で、タイプ: matchEnd.Event:Wait()


    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()
    -- ゲームの長さを待つプレースホルダー。
    matchEnd.Event:Wait()
    end
  3. テスト ゲーム。プレイヤーがアリーナに入ったとき、インターミッションループ 続かないことを確認します。スクリプトは現在、matchEnd シグナルが発射するのを待っています。

トラブルシュートのヒント

この時点では、コードが期待通りに機能しないので、以下のいずれかを試してください。

  • matchEnd.Event:Wait() でドットまたはコロン演算子の使用を二度チェックします。
  • MatchEnd がバインド可能なイベントであり、リモートイベントなどの別のタイプではないことを確認してください。

タイマーを使用

マッチの終了を引き起こす条件の 1つは、スクリプトを通じて処理されるタイマー切れです。

タイマーを設定する

ゲームにタイマーを追加するには、以下のステップでプレイスドモジュールスクリプトを使用してください。タイマーを開始および終了する機能や、残り時間の量を返す機能などが含まれています。

  1. ServerStorage > ModuleScripts で、タイマーという名前の新しいモジュールスクリプトを作成します。

    コードを以下のコードで置き換えます。


    local Timer = {}
    Timer.__index = Timer
    function Timer.new()
    local self = setmetatable({}, Timer)
    self._finishedEvent = Instance.new("BindableEvent")
    self.finished = self._finishedEvent.Event
    self._running = false
    self._startTime = nil
    self._duration = nil
    return self
    end
    function Timer:start(duration)
    if not self._running then
    task.spawn(function()
    self._running = true
    self._duration = duration
    self._startTime = tick()
    while self._running and tick() - self._startTime < duration do
    task.wait()
    end
    local completed = self._running
    self._running = false
    self._startTime = nil
    self._duration = nil
    self._finishedEvent:Fire(completed)
    end)
    else
    warn("Warning: timer could not start again as it is already running.")
    end
    end
    function Timer:getTimeLeft()
    if self._running then
    local now = tick()
    local timeLeft = self._startTime + self._duration - now
    if timeLeft < 0 then
    timeLeft = 0
    end
    return timeLeft
    else
    warn("Warning: could not get remaining time, timer is not running.")
    end
    end
    function Timer:isRunning()
    return self._running
    end
    function Timer:stop()
    self._running = false
    end
    return Timer
  2. マッチマネージャでは、ゲーム設定とタイマモジュールが必要です。


    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"))
  3. 変数の下で、変数名が と同じの新しいタイマーオブジェクトを作成します。このオブジェクトは、タイマーを開始および停止する関数を呼び出すのに使用されます。


    local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
    local timer = require(moduleScripts:WaitForChild("Timer"))
    -- マッチ時間を追跡するために使用される新しいタイマーオブジェクトを作成します。
    local myTimer = timer.new()

開始と停止

タイマーが作成されたので、マッチ中に含まれた機能 start()stop() を使用します。以下は、それぞれの機能と受け入れるパラメータの説明です。

  • start(time) - タイマーを開始し、秒単位の時間をパラメータとします。
  • finished:Connect(functionName) - タイマーが終了すると、パラメータとして渡された関数を実行します。
  1. In マッチマネージャー , create a new function named timeUp() をタイマーが終了したときに実行する新しい機能を作成します。テスト印刷文を含めます。


    local myTimer = timer.new()
    -- ローカル機能
    local function timeUp()
    print("Time is up!")
    end
    -- モジュール機能
    function MatchManager.prepareGame()
    playerManager.sendPlayersToMatch()
    end
    return MatchManager
  2. 以下の timeUp() 、print 文を使用して名前の startTimer() の機能を追加します。後でゲーム内でタイマーを表示します。


    -- ローカル機能
    local function timeUp()
    print("Time is up!")
    end
    local function startTimer()
    print("Timer started")
    end
  3. タイマーを開始および停止するには、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

タイマーを開始

タイマーは、マッチ開始イベントを使用して、試合開始時にトリガーできます。

  1. マッチマネージャーでは、モジュール変数の下で、イベントフォルダ、マッチスタート、マッチエンド(将来のレッスンで使用される)を保存する変数を作成します。


    -- モジュールスクリプト
    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()
  2. return MatchManager 、マッチ開始イベントを startTimer() に接続します。


    -- モジュール機能
    function MatchManager.prepareGame()
    playerManager.sendPlayersToMatch()
    end
    matchStart.Event:Connect(startTimer)
    return MatchManager
  3. マッチ開始イベントを発射するには、prepareGame()matchStart:Fire() をタイプします。


    -- モジュール機能
    function MatchManager.prepareGame()
    playerManager.sendPlayersToMatch()
    matchStart:Fire()
    end
  4. ゲームをテストします。出力ウィンドウで、タイマーの開始と停止機能の印刷文を見ることができるかどうかを確認します。

完了したスクリプト

以下は、作業を確認するための完了したスクリプトです。

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 do
repeat
task.wait(gameSettings.intermissionDuration)
print("Restarting intermission")
until #Players:GetPlayers() >= gameSettings.minimumPlayers
print("Intermission over")
task.wait(gameSettings.transitionTime)
matchManager.prepareGame()
-- ゲームの長さを待つプレースホルダー。
matchEnd.Event:Wait()
end