サイトアイコン schilverberch★ROBLOX

第18回 ~タッチイベント Touched~

ROBLOX★プログラミング講座【入門編】第18回 ~タッチイベント Touched~

イベントというものを理解しましょう。最初のイベントは「タッチイベント」というもので、プレイヤーがパーツに触れると発生するものです。

登場する英単語

英単語読み方
Touchedタッチド別のパーツに触れたら発生するイベント接続用オブジェクト
Connectコネクトイベントを接続する Touchedの関数
Humanoidヒューマノイドオブジェクト
HealthヘルスHumanoidのプロパティ、体力値、これを0にすると絶命
RightFootライトフット「右足」 Humanoidの1パーツ
LeftFootレフトフット「左足」  Humanoidの1パーツ
LeftLowerLegレフトロウレグ「左下腿」  Humanoidの1パーツ
RightLowerLegライトロウレグ「右下腿」  Humanoidの1パーツ
FindFirstChildWhichIsA ファインド・ファースト・チャイルド・フィッチイズエー指定したクラスを子供の中から探す

チェックポイント

local function onTouch(hit)
    print(hit.Name)
end

script.Parent.Touched:Connect(onTouch)
local function onTouch(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChild("Humanoid")

    if humanoid then
        humanoid.Health = 0
    end
end

script.Parent.Touched:Connect(onTouch)
local function onTouch(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")

    if humanoid then
        humanoid.Health = 0
    end
end

script.Parent.Touched:Connect(onTouch)
script.Parent.Touched:Connect(function(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")

    if humanoid then
        humanoid.Health = 0
    end
end)
モバイルバージョンを終了