[모듈 공유] 타워 게임 - Damage 파트 만들기

beans·2023년 5월 13일
0

1. ServerScriptServiceScript 추가

2. Script 이름 BlockDamageScript로 변경

3. BlockDamageScript에 아래 코드 붙여넣기




-- Script by bean7189



-- 이 스크립트는 캐릭터에게 데미지를 주는 스크립트 입니다.
-- Workspace 내에 존재하는 모든 파트들 중 이름을 "Damage"로 하면 기본 10의 데미지가, "Damage-25"로 하면 25의 데미지, "Damage-100"으로 하면 캐릭터를 죽일 수 있습니다.



--// Functions //--

-- 데미지 블록에 닿았을 때 호출되는 함수.
local function OnDamageBlockTouch(coll: BasePart, nameTable: {})
    local character: Model = coll.Parent
    if not character then return end
    local Humanoid = character:FindFirstChildOfClass('Humanoid')
    if not Humanoid then return end
    if not (Humanoid.Health > 0) then return end

    -- 기본 데미지 10
    local amount = 10
    if nameTable[2] then amount = tonumber(nameTable[2]) end

    -- 데미지 주기
    Humanoid:TakeDamage(amount)
    -- 데미지 쿨타임
    task.wait(0.25)
end

-- Workspace 내에 있는 모든 Part를 찾아 이름에 Damage가 있을 경우 데미지를 줄 수 있도록 작성된 함수.
local function InitializeDamageBlocks()
    for _, block: BasePart in pairs(workspace:GetDescendants()) do
        if not block:IsA('BasePart') then continue end
        if not string.find(block.Name, 'Damage') then continue end
        local nameTable = string.split(block.Name, '-')

        local debounce = false
        block.Touched:Connect(function(coll)
            if debounce then return end

            debounce = true
            -- 데미지 부여하는 함수
            OnDamageBlockTouch(coll, nameTable)
            debounce = false
        end)
    end
end

----



--// Main //--
-- Workspace 내 모든 데미지 블록에 기능 부여
InitializeDamageBlocks()
----

4. 데미지 파트 만들기

  1. 파트 3개 생성

  2. 색 및 재질 변경

  3. 이름 변경 (매우 중요)

5. 탐색기 상 데미지 블록 관련 전체 모습

0개의 댓글