시작하기
중급 튜토리얼

건강 회복 아이템 만들기

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

기본 스크립팅 튜토리얼을 통해 플레이 가능한 장면을 만들기 위해 개별 부품을 스크립팅했습니다. 이전 방법에서는 부품을 복제하면 스크립트도 복제되기 때문에 스크립트를 업데이트하는 것이 번거로울 수 있습니다. 각 스크립트를 따로 수정해야 하기 때문입니다.

이번 튜토리얼에서는 건강 아이템을 여러 개 생성하는 패턴을 사용할 것이며, 이는 건강 아이템 동작을 결정하는 스크립트의 단일 복사본만으로 구현됩니다. 아이템에 접촉할 때 플레이어의 건강을 회복하고, 약간의 페이드 효과를 주며, 잠시 비활성화됩니다.

설정하기

먼저 건강 회복 아이템으로 사용할 부품이나 모델이 필요합니다. 쇼다운 타운 예제 월드에는 맵 곳곳에 많은 건강 회복 아이템이 포함되어 있습니다.

경험의 메인 페이지에서 스튜디오에서 수정 옵션

각 건강 회복 아이템은 두 개의 직사각형 부품의 유니온으로, 그 안에 초록색 PointLight가 들어 있습니다. 이 아이템들은 모두 HealthPickups라는 이름의 워크스페이스 내 폴더에 저장되어 있으며, 스크립트가 이 폴더에서 아이템을 찾습니다. 맵에 더 추가하려면 반드시 이 폴더에 저장해야 합니다.

건강 회복하기

먼저, 스크립트가 플레이어의 건강을 회복해야 합니다. 이 패턴은 치명적인 용암 튜토리얼에서 익숙할 것입니다.

  1. ServerScriptServicePickupManager라는 이름의 스크립트를 추가합니다.

  2. 이 스크립트에서 100이라는 값을 가진 MAX_HEALTH라는 상수를 선언합니다.

  3. 픽업에 접촉한 다른 부품과 해당 픽업을 매개변수로 갖는 onTouchHealthPickup라는 함수를 생성합니다.


    local MAX_HEALTH = 100
    local function onTouchHealthPickup(otherPart, healthPickup)
    end
  4. 함수 내에서 otherPart의 부모에서 캐릭터 모델을 가져옵니다. 다음으로 FindFirstChildWhichIsA()를 사용하여 Humanoid가 있는지 확인합니다.

  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를 사용하여 스크립트를 일시 중지하고 건강 회복 아이템이 로드될 때까지 대기할 수 있습니다.

폴더에서 호출된 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

  • Index: 이는 일반 for 루프의 제어 변수와 같습니다.
  • Value: 이는 루프가 반복됨에 따라 배열의 각 요소로 채워집니다. 가급적이면 실제로 포함될 내용을 기반으로 변수 이름을 지정하는 것이 좋습니다.
  • Array: 반복하려는 배열이 ipairs 함수에 전달됩니다.

다음 코드에서는 인덱스가 필요하지 않으므로 _로 비워둘 수 있습니다. ipairs 함수를 사용하여 healthPickups를 전달하는 for 루프를 생성합니다.


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 루프 내에서 Touched 이벤트를 익명의 함수에 연결합니다. 이 함수는 otherPart라는 매개변수를 가집니다.

  2. onTouchHealthPickups 함수를 호출하여 otherPart 매개변수와 healthPickup을 전달합니다.


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

이제 코드를 테스트하면 건강 회복 아이템이 플레이어의 건강을 회복하게 될 것입니다. 먼저 플레이어에게 피해를 주어야 합니다 - 스폰 포인트 옆의 통풍구에 서보세요.

스폰 포인트 오른쪽의 예제 월드에 있는 증기 통풍구

오른쪽 상단에 건강 바가 나타나야 하며, 플레이어가 치유되면 사라집니다.

픽업 쿨다운

현재 건강 회복 아이템은 접촉하는 어떤 플레이어에게도 무한대로 회복 효과를 제공합니다. 아이템은 한 번만 획득 가능하고, 다시 사용할 수 있기 전 짧은 쿨다운을 가질 수 있다면 더 효과적일 것입니다.

먼저, 픽업이 쿨다운 기간에 있는지 여부를 기록해야 합니다. 아래 패턴은 Fade Trap에서 익숙할 것입니다 - 이번에는 쿨다운을 건강 회복 아이템에 속성을 설정하여 달성합니다.

  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. 스크립트의 상단에 세 개의 상수를 선언합니다 (각 값을 원하는 대로 조정하세요):

    • 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'은 미국 및 기타 국가 내 당사의 등록 및 미등록 상표입니다.