플레이어의 체력을 감소시키는 함정은 조건부 문으로 코딩할 수 있는 재미있는 게임 플레이 요소입니다.터치하면 플레이어의 체력을 0으로 설정하는 부품을 만들어 조건부 사용을 연습하세요.
트랩 설정
트랩은 이동 기반 도전과 같은 경험에서 특히 잘 작동합니다, 오비와 같은.이 단계는 필요한 변수와 함수를 설정하여 시작됩니다.코드 상자를 먼저 보지 않고 가능한 한 많이 수행하십시오.
트랩 부품을 만들고 이름을 지정합니다. 부품에 스크립트를 삽입합니다.
스크립트에서 설명 주석을 추가한 다음 변수를 사용하여 스크립트의 부모를 참조합니다.
-- 플레이어가 이 부분을 터치하면 체력을 0으로 설정local trapPart = script.Parent매개 변수의 이름이 onTouch() 인 함수를 생성하고 매개 변수의 이름이 otherPart 인 매개 변수를 생성합니다.
-- 플레이어가 이 부분을 터치하면 체력을 0으로 설정local trapPart = script.Parentlocal function onTouch(otherPart)end함수를 트랩 부분의 Touched 이벤트에 연결하여 부품에 무언가가 접촉할 때마다 실행합니다.
local trapPart = script.Parentlocal function onTouch(otherPart)endtrapPart.Touched:Connect(onTouch)
플레이어 터치 확인
기억하십시오, 매개 변수 otherPart는 플레이어의 일부 또는 기본 플레이트일 수 있는 트랩 부분에 접촉하는 모든 것을 기록합니다.
함정이 플레이어만 파괴하고 랜덤 장식 아이템을 파괴하지 않도록 하려면 if/then 문을 사용하여 otherPart에 있는 무엇이든 인간형 개체가 포함되어 있는지 확인하십시오.
특정 개체 찾기
함수 FindFirstChildWhichIsA()는 인간형 개체를 찾고 있기 때문에 유용하게 사용할 수 있는 특정 개체 유형을 찾는 데 사용할 수 있습니다.플레이어는 아바타의 일부만 트랩에 터치할 가능성이 높으므로 터치한 부분의 부모를 찾고 휴머노이드를 검색하기 위해 변수를 설정해야 합니다.
에서 onTouch() , type local character = otherPart.Parent .
local trapPart = script.Parentlocal function onTouch(otherPart)-- 다른 파트의 부모 개체 찾기local character = otherPart.ParentendtrapPart.Touched:Connect(onTouch)확인하려면 character에 Humanoid가 있는지 입력하세요:
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")endtrapPart.Touched:Connect(onTouch)
if 문으로 확인
인간형이 발견되면 인간형의 체력을 0으로 설정합니다.
if 문을 사용하여 휴머노이드가 local humanoid에 성공적으로 할당되었는지 확인합니다.
local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")-- Humanoid가 발견되었는지 평가합니다if humanoid thenendendtrapPart.Touched:Connect(onTouch)인쇄 문을 추가하고 지금까지의 코드를 확인합니다.
local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")-- Humanoid가 발견되었는지 평가합니다if humanoid thenprint("Found a Humanoid")endendtrapPart.Touched:Connect(onTouch)코드를 실행하고 플레이어가 부품을 만질 때마다 출력을 볼 수 있는지 확인합니다.
플레이어의 체력 변경
문이 참이면 동일한 휴머노이드 변수를 사용하여 플레이어의 체력을 0으로 설정할 수 있습니다.
사이의 then 와 end , 유형 humanoid.Health = 0 .
완료된 스크립트local trapPart = script.Parentlocal function onTouch(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")-- Humanoid가 발견되었는지 평가합니다if humanoid thenprint("Found a Humanoid")humanoid.Health = 0endendtrapPart.Touched:Connect(onTouch)트랩을 테스트합니다.
요약
이 트랩 부품은 조건을 사용하여 인간형 부품을 감지하고 인간형의 건강을 0으로 설정했습니다.이 스크립트는 터치하는 개체를 상관없이 파괴했던 이전 트랩 스크립트의 개선 버전입니다.
그러나 여전히 몇 가지 결함이 있습니다.인간형은 플레이어에만 있지 않습니다.인간형은 플레이 불가능한 캐릭터에서도 발견됩니다.스크립트는 플레이어의 건강을 0으로 설정하는 것만 좋습니다.소량의 체력을 빼는 실험을 할 수 있지만, 원하는 대로 체력을 빠르게 빼앗을 가능성이 높습니다.나중에 배우는 내용은 플레이어에게서 뺏기는 체력의 양을 더 많이 제어할 수 있도록 향상된 기능을 제공합니다.