함정으로 if/then 연습하기

*이 콘텐츠는 AI(베타)를 사용해 번역되었으며, 오류가 있을 수 있습니다. 이 페이지를 영어로 보려면 여기를 클릭하세요.

플레이어의 체력을 감소시키는 함정은 조건문으로 코딩할 수 있는 재미있는 게임 플레이 요소입니다. 접촉할 때 플레이어의 체력을 0으로 설정하는 파트를 만들어 조건문을 연습해 보세요.

함정 설정하기

함정은 오비와 같은 이동 기반 도전 과제가 있는 게임에서 매우 잘 작동합니다. 이 단계에서는 필요한 변수와 함수를 설정하는 것으로 시작합니다. 코드 상자를 먼저 보지 않고 할 수 있는 만큼 해보세요.

  1. 함정 파트를 만들고 이름을 지정합니다. 파트에 스크립트를 삽입합니다.

  2. 스크립트에서 설명적인 주석을 추가한 후, 스크립트의 부모를 참조하는 변수를 사용합니다.

    -- 플레이어가 이 파트를 터치하면 체력을 0으로 설정합니다.
    local trapPart = script.Parent
  3. onTouch()라는 이름의 함수를 만들고, 매개변수로 otherPart를 지정합니다.

    -- 플레이어가 이 파트를 터치하면 체력을 0으로 설정합니다.
    local trapPart = script.Parent
    local function onTouch(otherPart)
    end
  4. 함수를 함정 파트의 Touched 이벤트에 연결하여 무언가가 파트를 터치할 때마다 실행되도록 합니다.

    local trapPart = script.Parent
    local function onTouch(otherPart)
    end
    trapPart.Touched:Connect(onTouch)

플레이어 터치 확인하기

매개변수 otherPart는 함정 파트를 터치하는 모든 것을 기록합니다. 이는 플레이어의 일부일 수도 있고 단순히 바닥판일 수도 있습니다.

함정이 플레이어만 파괴하고 무작위 장식 아이템은 파괴하지 않도록 하려면, if/then 문을 사용하여 otherPart에 Humanoid 객체가 포함되어 있는지 확인합니다.

특정 객체 찾기

FindFirstChildWhichIsA() 함수를 사용하여 특정 객체 유형을 찾을 수 있습니다. 이는 Humanoid 유형의 객체를 찾고 있기 때문에 유용합니다. 플레이어는 아바타의 일부만으로 함정을 터치할 가능성이 높으므로, 터치한 파트의 부모를 찾고 Humanoid를 검색하기 위해 변수를 설정해야 합니다.

  1. onTouch()에서 local character = otherPart.Parent를 입력합니다.

    local trapPart = script.Parent
    local function onTouch(otherPart)
    -- otherPart의 부모 객체를 찾습니다.
    local character = otherPart.Parent
    end
    trapPart.Touched:Connect(onTouch)
  2. characterHumanoid가 있는지 확인하려면 다음을 입력합니다:

    local humanoid = character:FindFirstChildWhichIsA("Humanoid")

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

if 문으로 확인하기

Humanoid가 발견되면, Humanoid의 Health를 0으로 설정합니다.

  1. Humanoid가 local humanoid에 성공적으로 할당되었는지 확인하기 위해 if 문을 사용합니다.

    local trapPart = script.Parent
    local function onTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    -- Humanoid가 발견되었는지 평가합니다.
    if humanoid then
    end
    end
    trapPart.Touched:Connect(onTouch)
  2. print 문을 추가하고 지금까지의 코드를 확인합니다.

    local trapPart = script.Parent
    local function onTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    -- Humanoid가 발견되었는지 평가합니다.
    if humanoid then
    print("Humanoid를 찾았습니다.")
    end
    end
    trapPart.Touched:Connect(onTouch)
  3. 코드를 실행하고 플레이어가 파트를 터치할 때마다 출력이 보이는지 확인합니다.

플레이어의 체력 변경하기

문이 참이면, 동일한 humanoid 변수를 사용하여 플레이어의 체력을 0으로 설정할 수 있습니다.

  1. thenend 사이에 humanoid.Health = 0을 입력합니다.

    완료된 스크립트
    local trapPart = script.Parent
    local function onTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    -- Humanoid가 발견되었는지 평가합니다.
    if humanoid then
    print("Humanoid를 찾았습니다.")
    humanoid.Health = 0
    end
    end
    trapPart.Touched:Connect(onTouch)
  2. 함정을 테스트합니다.

요약

이 함정 파트는 조건문을 사용하여 Humanoid 부품을 감지하고 Humanoid의 체력을 0으로 설정했습니다. 이 스크립트는 어떤 접촉 객체든지 파괴했던 이전의 함정 스크립트보다 개선된 것입니다.

그러나 여전히 몇 가지 결함이 있습니다. Humanoid는 플레이어뿐만 아니라 NPC에도 존재합니다. 이 스크립트는 플레이어의 체력을 0으로 설정하는 데만 유용합니다. 체력을 소량 감소시키는 실험을 할 수 있지만, 원하는 것보다 더 빠르게 체력이 감소할 가능성이 높습니다. 이후 수업에서는 플레이어의 체력이 얼마나 감소하는지에 대한 더 큰 제어를 제공하는 추가 개선 사항을 제공합니다.

©2026 Roblox Corporation. Roblox 및 Roblox 로고, 'Powering Imagination'은 미국 및 기타 국가 내 당사의 등록 및 미등록 상표입니다.