始める
中級者向けチュートリアル

ヘルスピックアップを作成する

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

基本的なスクリプティングのチュートリアルを通じて、あなたは遊べるシーンを作成するために個々のパーツをスクリプトしました。以前の方法では、パーツを複製すると、スクリプトも複製されてしまいます。このため、スクリプトを更新するのが面倒で、変更がスクリプトごとに行わなければなりませんでした。

このチュートリアルでは、ヘルスピックアップをいくつか作成するために異なるパターンを使用します。スクリプトは1つだけ用意し、ヘルスピックアップの動作を決定します。ピックアップが触れられると、プレイヤーの健康が回復し、わずかに消え、短期間無効になります。

セットアップ

まず、ピックアップとして使用するパーツまたはモデルが必要です。ショーダウンタウンの例の世界には、マップ全体に広がる多くのヘルスピックアップがあります。

体験のメインページからのスタジオでの編集オプション

各ヘルスピックアップは、内側に緑のPointLightがある2つの長方形パーツのユニオンです。これらすべては、スクリプトがそれらを探しているHealthPickupsというワークスペース内の1つのフォルダに保存されています。マップにさらに追加する場合、必ずこのフォルダ内に保存されていることを確認してください。

ヘルスの回復

まず、スクリプトがプレイヤーの健康を回復する必要があります。このパターンはデッドリーラヴァチュートリアルからおなじみのものであるはずです。

  1. ServerScriptServicePickupManagerという名前のスクリプトを追加します。

  2. このスクリプトの中で、MAX_HEALTHという定数を100の値で宣言します。

  3. ピックアップに触れた他のパーツのパラメータとピックアップを使ってonTouchHealthPickupという関数を作成します。


    local MAX_HEALTH = 100
    local function onTouchHealthPickup(otherPart, healthPickup)
    end
  4. 関数内で、otherPartの親からキャラクターモデルを取得します。次に、それがHumanoidを持っているかをFindFirstChildWhichIsA()を用いて確認します。

  5. もしヒューマノイドがあれば、そのHealthプロパティをMAX_HEALTHに設定します。


    local MAX_HEALTH = 100
    local function onTouchHealthPickup(otherPart, healthPickup)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
    humanoid.Health = MAX_HEALTH
    end
    end

ピックアップフォルダの取得

ヘルスピックアップを保持するフォルダは、スクリプトが実行される時点でまだ体験にロードされていない可能性があります。WaitForChildを使って、スクリプトを一時停止させ、HealthPickupsフォルダをロードされたときに取得できます。

フォルダに対して呼び出されると、GetChildren関数はフォルダの内容の配列を返します。

  1. MAX_HEALTHの下にhealthPickupsFolderという変数を宣言し、WaitForChild関数を使ってワークスペースからHealthPickupsフォルダを取得します。

  2. healthPickupsFolderに対してGetChildren関数を呼び出した結果を保存するための変数healthPickupsを作成します。


    local MAX_HEALTH = 100
    local healthPickupsFolder = workspace:WaitForChild("HealthPickups")
    local healthPickups = healthPickupsFolder:GetChildren()
    local function onTouchHealthPickup(otherPart, healthPickup)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
    humanoid.Health = MAX_HEALTH
    end
    end

ipairsを使ったループ

onTouchHealthPickupは、配列内の各ヘルスピックアップに対して呼び出される必要があります。これを効率的に行うために、新しい種類のループ構文を使用します。

ipairsは、forループと一緒に使用して配列の各要素を通過させるための関数です。ループの開始位置と終了位置を指定する必要はありません。ipairsを使用したforループの定義は次の通りです。

for index, value in ipairs(array) do

  • インデックス: これは通常のforループの制御変数に相当します。
  • : ループが繰り返されるにつれて、配列の各要素がこの変数に格納されます。実際に含むものに名前を付けるのが良いアイデアです。
  • 配列: イテレートしたい配列がipairs関数に渡されます。

次のコードでは、インデックスは何のためにも必要ないため、_で空白にできます。ipairs関数を使用してforループを作成し、healthPickupsを渡します。


local function onTouchHealthPickup(otherPart, healthPickup)
local character = otherPart.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health = MAX_HEALTH
end
end
for _, healthPickup in ipairs(healthPickups) do
end

ヘルスピックアップをonTouchHealthPickup関数に渡すラッパー関数が必要になります。

  1. forループの中で、Toggledイベントを引数にotherPartというパラメータを持つ匿名関数に接続します。

  2. onTouchHealthPickup関数を呼び出し、otherPartの引数とhealthPickupの両方を渡します。


    for _, healthPickup in ipairs(healthPickups) do
    healthPickup.Touched:Connect(function(otherPart)
    onTouchHealthPickup(otherPart, healthPickup)
    end)
    end

今コードをテストすると、ヘルスピックアップがあなたのヘルスを回復することが分かります。まずあなたのキャラクターをダメージを与える必要があります - スポーンポイントの隣にある通風口の上に立ってみてください。

スポーンポイントの右側にある例の世界の間欠的な通風口

右上にヘルスバーが表示され、プレイヤーが回復すると消えます。

ピックアップのクールダウン

現在、ピックアップはそれを触れたプレイヤーを無限に回復します。一度だけ収集できるようにし、再び使用できる前に短いクールダウンを設けると効果的です。

最初に、ピックアップがクールダウン期間内にあるかどうかを記録する必要があります。以下のパターンは フェードトラップからおなじみのものであります - 今回は、ヘルスピックアップに属性を設定することでデバウンスを実現します。

  1. forループの中で、"Enabled"という新しい属性trueに設定します。

  2. onTouchHealthPickup内のコードを、healthPickup:GetAttribute("Enabled")の条件があるifステートメントで囲みます。


    local function onTouchHealthPickup(otherPart, healthPickup)
    if healthPickup:GetAttribute("Enabled") then
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
    humanoid.Health = MAX_HEALTH
    end
    end
    end
    for _, healthPickup in ipairs(healthPickups) do
    healthPickup:SetAttribute("Enabled", true)
    healthPickup.Touched:Connect(function(otherPart)
    onTouchHealthPickup(otherPart, healthPickup)
    end)
    end

ピックアップを無効にする

ピックアップは無効であることを視覚的に示すべきです - 無効を示す一般的な方法は、わずかに透明にすることです。

  1. スクリプトの最上部に、次の3つの定数を宣言します(それぞれの値はお好みに合わせて調整してください):

    • ENABLED_TRANSPARENCY = 0.4
    • DISABLED_TRANSPARENCY = 0.9
    • COOLDOWN = 10

    local MAX_HEALTH = 100
    local ENABLED_TRANSPARENCY = 0.4
    local DISABLED_TRANSPARENCY = 0.9
    local COOLDOWN = 10
    local healthPickupsFolder = workspace:WaitForChild("HealthPickups")
  2. onTouchHealthPickup内のifステートメントで、ピックアップのTransparencyDISABLED_TRANSPARENCYに設定し、Enabled属性の値をfalseに設定します。


    local function onTouchHealthPickup(otherPart, healthPickup)
    if healthPickup:GetAttribute("Enabled") then
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
    humanoid.Health = MAX_HEALTH
    healthPickup.Transparency = DISABLED_TRANSPARENCY
    healthPickup:SetAttribute("Enabled", false)
    end
    end
    end
  3. task.wait()関数を呼び出し、引数にCOOLDOWNを渡します。

  4. TransparencyENABLED_TRANSPARENCYに戻し、Enabledtrueに戻します。


    local function onTouchHealthPickup(otherPart, healthPickup)
    if healthPickup:GetAttribute("Enabled") then
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
    humanoid.Health = MAX_HEALTH
    healthPickup.Transparency = DISABLED_TRANSPARENCY
    healthPickup:SetAttribute("Enabled", false)
    task.wait(COOLDOWN)
    healthPickup.Transparency = ENABLED_TRANSPARENCY
    healthPickup:SetAttribute("Enabled", true)
    end
    end
    end

ピックアップを再度テストしてみてください:ピックアップに触れると、ヘルスが回復し、透明になり、再び使用できるようになります。

プレイヤーがピックアップを収集したときのフィードバックを向上させたければ、透明度を変更したときにピックアップのPointLightの明るさを削減してみてください。

これらのヘルスピックアップをあなた自身のプロジェクトで使用するか、外観や効果を変更してプレイヤーに異なるタイプのパワーアップを提供してみてください。

最終コード


local MAX_HEALTH = 100
local ENABLED_TRANSPARENCY = 0.4
local DISABLED_TRANSPARENCY = 0.9
local COOLDOWN = 10
local healthPickupsFolder = workspace:WaitForChild("HealthPickups")
local healthPickups = healthPickupsFolder:GetChildren()
local function onTouchHealthPickup(otherPart, healthPickup)
if healthPickup:GetAttribute("Enabled") then
local character = otherPart.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health = MAX_HEALTH
healthPickup.Transparency = DISABLED_TRANSPARENCY
healthPickup:SetAttribute("Enabled", false)
task.wait(COOLDOWN)
healthPickup.Transparency = ENABLED_TRANSPARENCY
healthPickup:SetAttribute("Enabled", true)
end
end
end
for _, healthPickup in ipairs(healthPickups) do
healthPickup:SetAttribute("Enabled", true)
healthPickup.Touched:Connect(function(otherPart)
onTouchHealthPickup(otherPart, healthPickup)
end)
end
©2026 Roblox Corporation。Roblox(ロブロックス)、RobloxロゴおよびPowering Imaginationは、米国並びにその他の国における登録商標および非登録商標です。