디바운스 패턴

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

A 디바운스 패턴은 함수가 너무 많은 횟수로 실행되거나 입력이 여러 번 트리거되는 것을 방지하는 코딩 기법입니다. 다음 스크립팅 시나리오는 디바운스를 모범 사례로 보여줍니다.

충돌 감지

위험한 트랩 파트를 생성하여 터치할 때 10의 피해를 입히고 싶다고 가정해 보겠습니다. 초기 구현은 기본 BasePart.Touched 연결과 다음과 같은 damagePlayer 함수를 사용할 수 있습니다:

Script - 피해 주기

local part = script.Parent
local function damagePlayer(otherPart)
print(part.Name .. " 충돌했습니다 " .. otherPart.Name)
local humanoid = otherPart.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health -= 10 -- 플레이어 건강 감소
end
end
part.Touched:Connect(damagePlayer)

처음 보기에는 논리적으로 보이지만, 테스트 결과 Touched 이벤트가 미세한 물리적 충돌에 따라 빠른 연속으로 여러 번 발생한다는 것을 알 수 있습니다.

초기 접촉 시 과도한 피해를 초래하지 않기 위해, 인스턴스 속성을 통해 피해에 대한 쿨다운 기간을 적용하는 디바운스 시스템을 추가할 수 있습니다.

Script - 디바운스를 사용하여 피해 주기

local part = script.Parent
local RESET_TIME = 1
local function damagePlayer(otherPart)
print(part.Name .. " 충돌했습니다 " .. otherPart.Name)
local humanoid = otherPart.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
if not part:GetAttribute("Touched") then
part:SetAttribute("Touched", true) -- 속성을 true로 설정
humanoid.Health -= 10 -- 플레이어 건강 감소
task.wait(RESET_TIME) -- 리셋 기간 대기
part:SetAttribute("Touched", false) -- 속성 리셋
end
end
end
part.Touched:Connect(damagePlayer)

사운드 트리거

디바운스는 두 개의 파트가 충돌할 때 사운드를 재생하거나, 사용자가 화면 버튼과 상호작용할 때 Activated 이벤트에서 사운드를 재생하는 것과 같은 사운드 효과 작업에도 유용합니다. 두 경우 모두 Sound:Play()를 호출하면 트랙의 시작부터 재생이 시작되며, 디바운스 시스템이 없다면 소리가 빠른 연속으로 여러 번 재생될 수 있습니다.

사운드 오버랩을 방지하기 위해 Sound 객체의 IsPlaying 속성을 사용하여 디바운스를 적용할 수 있습니다:

Script - 디바운스를 사용하여 충돌 사운드 재생

local projectile = script.Parent
local function playSound()
-- 파트의 자식 사운드 찾기
local sound = projectile:FindFirstChild("Impact")
-- 이미 재생 중이지 않을 때만 사운드를 재생
if sound and not sound.IsPlaying then
sound:Play()
end
end
projectile.Touched:Connect(playSound)
Script - 디바운스를 사용하여 버튼 클릭 재생

local button = script.Parent
local function onButtonActivated()
-- 버튼의 자식 사운드 찾기
local sound = button:FindFirstChild("Click")
-- 이미 재생 중이지 않을 때만 사운드를 재생
if sound and not sound.IsPlaying then
sound:Play()
end
end
button.Activated:Connect(onButtonActivated)

수집 효과

게임에는 종종 의료 키트, 탄약 팩 등과 같은 수집 가능한 픽업이 3D 세계에 포함됩니다. 이러한 픽업을 플레이어가 다시 가져갈 수 있도록 세계에 유지하도록 설계한 경우, 픽업이 새로 고쳐지고 재활성화되기 전에 "쿨다운" 시간을 추가해야 합니다.

충돌 감지를 관리하는 것과 비슷하게, 인스턴스 속성으로 디바운스 상태를 관리하고, 파트의 Transparency를 변경하여 쿨다운 기간을 시각화할 수 있습니다.

Script - 디바운스를 사용하여 건강 회복 픽업

local part = script.Parent
part.Anchored = true
part.CanCollide = false
local COOLDOWN_TIME = 5
local function healPlayer(otherPart)
local humanoid = otherPart.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
if not part:GetAttribute("CoolingDown") then
part:SetAttribute("CoolingDown", true) -- 속성을 true로 설정
humanoid.Health += 25 -- 플레이어 건강 증가
part.Transparency = 0.75 -- 쿨다운 상태를 표시하기 위해 반투명으로 설정
task.wait(COOLDOWN_TIME) -- 쿨다운 기간 대기
part.Transparency = 0 -- 파트를 완전히 불투명으로 리셋
part:SetAttribute("CoolingDown", false) -- 속성 리셋
end
end
end
part.Touched:Connect(healPlayer)
©2026 Roblox Corporation. Roblox 및 Roblox 로고, 'Powering Imagination'은 미국 및 기타 국가 내 당사의 등록 및 미등록 상표입니다.