デバウンスパターン

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

A デバウンス パターンは、関数が何度も実行されるのを防ぐコーディング技術であり、また入力が複数回トリガーされるのを防ぐものです。以下のスクリプティングシナリオは、デバウンスをベストプラクティスとして示しています。

衝突の検出

危険なトラップパートを作成し、触れたときに10のダメージを与えたいとしましょう。初期の実装は、基本的な BasePart.Touched 接続と damagePlayer 関数を次のように使用するかもしれません:

Script - Damage Player

local part = script.Parent
local function damagePlayer(otherPart)
print(part.Name .. " が " .. otherPart.Name .. " と衝突しました")
local humanoid = otherPart.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health -= 10 -- プレイヤーの健康を減少
end
end
part.Touched:Connect(damagePlayer)

一見論理的に見えますが、テストすると Touched イベントが微妙な物理的衝突に基づいて短時間で複数回発火することが示されるでしょう。

初期接触時に過剰なダメージを引き起こさないように、ダメージにクールダウン期間を強制するデバウンスシステムを追加できます。そのためには インスタンス属性 を使用します。

Script - Damage Player Using Debounce

local part = script.Parent
local RESET_TIME = 1
local function damagePlayer(otherPart)
print(part.Name .. " が " .. otherPart.Name .. " と衝突しました")
local humanoid = otherPart.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
if not part:GetAttribute("Touched") then
part:SetAttribute("Touched", true) -- 属性を true に設定
humanoid.Health -= 10 -- プレイヤーの健康を減少
task.wait(RESET_TIME) -- リセット期間を待つ
part:SetAttribute("Touched", false) -- 属性をリセット
end
end
end
part.Touched:Connect(damagePlayer)

サウンドのトリガー

デバウンスは、サウンドエフェクトを扱う際にも便利です。たとえば、2つのパートが衝突したときにサウンドを再生する (Touched) や、ユーザーが画面上のボタンと対話したときに Activated イベントでサウンドを再生する場合などです。この両方のケースでは、Sound:Play() はトラックの最初から再生を開始し、 — デバウンスシステムがなければ — サウンドが短時間で複数回再生される可能性があります。

サウンドの重複を防ぐために、IsPlaying プロパティを使用してデバウンスができます:

Script - Play Collision Sound Using Debounce

local projectile = script.Parent
local function playSound()
-- パートにある子サウンドを探す
local sound = projectile:FindFirstChild("Impact")
-- すでに再生中でない場合のみサウンドを再生
if sound and not sound.IsPlaying then
sound:Play()
end
end
projectile.Touched:Connect(playSound)
Script - Play Button Click Using Debounce

local button = script.Parent
local function onButtonActivated()
-- ボタンにある子サウンドを探す
local sound = button:FindFirstChild("Click")
-- すでに再生中でない場合のみサウンドを再生
if sound and not sound.IsPlaying then
sound:Play()
end
end
button.Activated:Connect(onButtonActivated)

ピックアップ効果

ゲームには、メディキットや弾薬パックなどのコレクション可能なピックアップが3D世界に存在することがよくあります。これらのピックアップがプレイヤーに再度取得できるように世界に残るように設計する場合、ピックアップがリフレッシュされ再アクティブ化される前に「クールダウン」時間を追加する必要があります。

衝突の検出 と同様に、デバウンス状態を インスタンス属性 で管理し、パートの Transparency を変更することでクールダウン期間を視覚化できます。

Script - Health Pickup Using Debounce

local part = script.Parent
part.Anchored = true
part.CanCollide = false
local COOLDOWN_TIME = 5
local function healPlayer(otherPart)
local humanoid = otherPart.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
if not part:GetAttribute("CoolingDown") then
part:SetAttribute("CoolingDown", true) -- 属性を true に設定
humanoid.Health += 25 -- プレイヤーの健康を増加
part.Transparency = 0.75 -- クールダウン状態を示すためパートを半透明にする
task.wait(COOLDOWN_TIME) -- クールダウン期間を待つ
part.Transparency = 0 -- パートを完全に不透明にリセット
part:SetAttribute("CoolingDown", false) -- 属性をリセット
end
end
end
part.Touched:Connect(healPlayer)
©2026 Roblox Corporation。Roblox(ロブロックス)、RobloxロゴおよびPowering Imaginationは、米国並びにその他の国における登録商標および非登録商標です。