최근 수정 시각 : 2025-08-16 14:02:04

Roblox Studio/Script(목록)


파일:상위 문서 아이콘.svg   상위 문서: Roblox Studio
파일:Roblox 로고 화이트.svg
{{{#!wiki style="margin: 0 -10px -5px; min-height: calc(1.5em + 5px)"
{{{#!folding [ 펼치기 · 접기 ]
{{{#!wiki style="margin: -5px -1px -11px; word-break: keep-all"
<colbgcolor=#335fff><colcolor=#fff> 게임 시스템 · 게임 목록 · 이벤트 · 유료 서비스
Studio Roblox Studio · 스크립트 · 스크립트 목록 · 무료 모델 · 유료 모델
기타 평가(긍정적) · 문제점 및 비판 · 논란 및 사건 사고 · Roblox Corporation · 중국 전용 플랫폼罗布乐思 · 2차 창작 · Myth · Roblox 프로젝트
}}}}}}}}} ||

1. 개요2. 대미지 블럭3. 승강기(방방이)

1. 개요

2. 대미지 블럭

'대미지 블럭'이라고 하는 것은 캐릭터가 닿았을 때 체력이 줄어드는 블럭을 말한다. 점프맵에서 자주 볼 수 있으며, 점프맵이 아니더라도 종종 볼 수 있다.[1] 블럭의 한 종류로 볼 수도 있지만, 대미지 블럭이라는 것이 따로 존재하는 것은 아니다.

일반적인 블럭에 닿았을 시 체력이 닳도록 구현하는 것이 목표이다. 만약 특정한 블럭 하나에 대해서만 구현하고 싶으면 예시 코드1과 같이 작성할 수 있다. 이때, 스크립트는 해당 블럭의 자식이어야 한다.


예시 코드1 [ 펼치기 · 접기 ]
#!syntax lua
local part = script.Parent -- 대미지 블럭

local debounce = false -- 버그 방지, 일종의 쿨타임

local DAMAGE = 10      -- 닿았을 때 받을 대미지
local RESET_TIME = 1   -- debounce의 시간

local function onTouch(hit)
	local character = hit.Parent
	local humanoid = hit.Parent:FindFirstChild("Humanoid")

	if character == workspace or not humanoid then
		return
	end

	if debounce then
		return
	end

	debounce = true
	humanoid:TakeDamage(DAMAGE)

	task.wait(RESET_TIME)
	debounce = false
end

part.Touched:Connect(onTouch) -- 블럭에 닿았을 때 'onTouch' 함수 실행


Debounce 변수로 코드 흐름을 제어하는 이유는 Touched 이벤트가 수십 밀리초 만에 수십 번 발생할 수 있기 때문이다. Debounce가 없으면 Damage를 5로 두어도, 한 번에 50씩 감소하는 것처럼 보이게 된다.

그러나, 두 명 이상이 거의 동시에 블럭에 닿게 된다면 한 명을 제외한 나머지는 체력이 닳지 않을 것이다. 로컬 스크립트가 아니므로 Debounce 변수를 다 같이 사용하기 때문이다. 이를 해결하기 위해 블럭에 닿은 플레이어를 테이블에 저장한다. 사람마다 Debounce를 따로 사용하게 되는 것이다. 이를 구현한 것이 예시 코드2이다.


예시 코드2 [ 펼치기 · 접기 ]
#!syntax lua
local part = script.Parent

local DAMAGE = 10
local RESET_TIME = 1

local touched = {} -- 닿은 플레이어를 저장할 테이블

local function onTouch(hit)
	local character = hit.Parent
	local humanoid = hit.Parent:FindFirstChild("Humanoid")

	if character == workspace or not humanoid then
		return
	end

	local player = game.Players:GetPlayerFromCharacter(character)
	if player and not touched[player] then

		touched[player] = true -- 테이블에 추가
		humanoid:TakeDamage(DAMAGE)

		task.delay(RESET_TIME, function()
			touched[player] = nil -- 테이블에서 삭제
		end)
	end
end

part.Touched:Connect(onTouch)


만약 '닿으면 바로 죽게 만드는 블럭'[2]을 구현하고자 한다면 Debounce가 필요 없기는 하다. 체력이 0이 되면 죽는 것이기 때문에 :TakeDamage 메서드를 사용하기 보다는 체력의 값을 0으로 직접 대입시키는 것이 더 낫다.

3. 승강기(방방이)

승강기 블럭은 플레이어가 물체외 접촉했을 때 플레이어를 위로 향하게 하는 물질이다. 이 블럭을 만들때는 CanCollide(통과)를 꺼줘야 작동한다.

예시 코드 [ 펼치기 · 접기 ]
#!syntax lua
local speed = 50 --올라가는 스피드
script.Parent.Touched:Connect(function(hit)
	local rootpart = hit.Parent:FindFirstChild("HumanoidRootPart") or hit
	
	if not rootpart:FindFirstChild("Elevator") and hit.Parent:FindFirstChild("Humanoid") then
		local velocity = Instance.new("BodyVelocity", rootpart)
		velocity.Name = "Elevator"
		local posi = script.Parent.CFrame.UpVector
		local vector = Vector3.new(0,math.huge,0)
		
		if posi.X ~= 0 then
			vector = Vector3.new(math.huge, vector.Y, vector.Z)
		end
		
		if posi.Z ~= 0 then
			vector = Vector3.new(vector.X, vector.Y, math.huge) 
		end
		
		velocity.MaxForce = vector
		velocity.Velocity = script.Parent.CFrame.UpVector * speed
	end
end)

[1] 예를 들면, 용암, 깨진 유리 등이 있다.[2] 보통 '킬파트'라고 한다.

분류