나무모에 미러 (일반/어두운 화면)
최근 수정 시각 : 2026-01-05 18:57:39

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,#335fff><colcolor=#fff,#fff> 게임 시스템 · 게임 목록 · 이벤트 · 유료 서비스
Roblox Studio 스크립트 · 스크립트 예시 목록 · 무료 모델 · 유료 모델
기타 평가 (긍정적) · 문제점 및 비판 · 논란 및 사건 사고 · Roblox Corporation · 중국 전용 플랫폼(罗布乐思) · 2차 창작 · Myth · Roblox 프로젝트
}}}}}}}}} ||

1. 개요2. 플레이어
2.1. 리더보드2.2. 추방
3. 인터페이스
3.1. CoreGui 관리3.2. 시스템 메시지
3.2.1. 공지 메시지
4. 점프맵
4.1. 대미지 블럭4.2. 승강기(방방이)

1. 개요

Roblox Studio의 예시 코드들을 모아놓은 문서이다.
필요시 약간 변형해서 사용할수도 있다.

2. 플레이어

2.1. 리더보드

리더보드란 게임내 점수 등을 저장해주는 Gui다.
아래 서술할 CoreGui를 세팅하지 않으면 자동으로 플레이어 목록 이나오고, 플레이어에 leaderstats라는 이름을 가진 개체가 있고,그 아래에 여러 Value들[1]이 있다면 표처럼 표시해 준다.
[리더보드 생성 스크립트 예시 보기]
#!syntax lua
local Players = game.Players
Players.PlayerAdded:Connect(function(player) -- 게임에 들어온 플레이어가 매개변수에 들어온다.
    local Leaderstats = Instance.new("Folder",player) --플레이어에 Folder를 생성한다.
    Leaderstats.Name = "leaderstats"
    
    local ScoreValue = Instance.new("IntValue,player") --플레이어에 IntValue를 생성한다
    ScoreValue.Name = "Score"
end)

2.2. 추방

플레이어가 부적절한 행동을 할때,또는로블록스 스튜디오 사이트에 차단하는 방법이 있지만 마음에 들지 않는 플레이어를 영구 추방[2] 할수도 있다.
[플레이어 추방 스크립트 예시 보기]
#!syntax lua
local Players = game.Players
Players.PlayerAdded:Connect(function(player)
    if player.Name == "플레이어 이름" then
        player:Kick("제작자가 당신을 추방했습니다.") --메세지와 함께 추방한다.
    end
end

3. 인터페이스

3.1. CoreGui 관리

'CoreGui 관리'란 리더보드나 채팅같은 사용 가능한 CoreGui를 스크립트에서 끄거나 키는 것으로 StarterGui를 불러와준 다음에
#!syntax lua
StarterGui:SetCoreGuiEnabled(coreGuiType, enabled)

를 넣어 coreGuiType에는 아래와 같은 CoreGuiType의 목록중 하나를 넣고 enabled을 false나 true로 정해 끄거나 킨다.

#!syntax lua
Enum.CoreGuiType.PlayerList --리더보드
Enum.CoreGuiType.Health --체력바
Enum.CoreGuiType.Backpack --인벤토리
Enum.CoreGuiType.Chat --채팅창
Enum.CoreGuiType.EmotesMenu --이모트창
Enum.CoreGuiType.SelfView --자기 캐릭터 얼굴 뜨는거
Enum.CoreGuiType.Captures --캡쳐

Enum.CoreGuiType.All --다

특히 캡쳐 기능은 누르면 잠시동안 캡쳐를 위해 Gui를 없애버리는 기능이 있어 Gui가 메인인 게임 등에서는 많이 꺼놓는다.

3.2. 시스템 메시지

TextChatService가 만약 정말 최신 것으로 바뀌어있다면 StarterGui:SetCore("ChatMakeSystemMessage")는 필요 없다.

시스템 메시지는 TextChannel의 DisplaySystemMessage() 메서드를 이용한다.
시스템 메시지는 클라이언트에만 작동하기 때문에 반드시 로컬스크립트로 작성해야한다.

'''
환영 메시지 예시 코드 1 [ 펼치기 · 접기 ]
#!syntax lua
local TextChatService = game:GetService("TextChatService") -- 채팅 서비스
local generalChannel = TextChatService:WaitForChild("TextChannels"):WaitForChild("RBXGeneral") -- 기본 채팅창 주소
if TextChatService.TextChannels:FindFirstChild("RBXGeneral") and game.Players.LocalPlayer then
	task.wait(1) -- 약간의 로드 텀
	generalChannel:DisplaySystemMessage('[시스템] 웰컴')
end
'''

3.2.1. 공지 메시지

공지 메시지는 업데이트 예고나 셧다운 예고, 게임 이벤트 등에 사용된다.
공지 메시지는 개발자나 관리자가 콘솔로 사용하거나 관리자 인터페이스를 만들어서 편하게 사용할 수 있다.

공지메시지를 하는 방법이다.
채팅창 공지메시지 예시 [ 펼치기 · 접기 ]
(스크립트)
#!syntax lua
game:GetService("MessagingService"):SubscribeAsync("Topic", function(Message) -- 공지메시지의 비밀번호 "Topic" 설정
	game:GetService("ReplicatedStorage").MessageEvent:FireAllClients(Message.Data.Message) -- 공지 메시지의 데이터를 로컬스크립트로
end)

(콘솔)
#!syntax lua
game:GetService("MessagingService"):PublishAsync("Topic", {Message = "자기가 하고 싶은 말"})

(로컬스크립트)
#!syntax lua
game:GetService("ReplicatedStorage").MessageEvent.OnClientEvent:Connect(function(Message) 
	local Text = string.format(Message) -- 메시지 문자열을 그대로 정렬하는 코드.
 	game:GetService("TextChatService"):WaitForChild("TextChannels"):WaitForChild("RBXGeneral"):DisplaySystemMessage(Text) -- 공지메시지를 채팅창에 띄우는 코드
end)

DisplaySystemMessage() 메서드로 채팅창에 MessageEvent에서 온 Message.Data를 띄우는 스크립트다.

4. 점프맵

4.1. 대미지 블럭

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

일반적인 블럭에 닿았을 시 체력이 닳도록 구현하는 것이 목표이다. 만약 특정한 블럭 하나에 대해서만 구현하고 싶으면 예시 코드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)


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

4.2. 승강기(방방이)

승강기 블럭은 플레이어가 물체외 접촉했을 때 플레이어를 위로 향하게 하는 물질이다. 이 블럭을 만들때는 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] IntValue,StringValue 등[2] 흔히 '밴' 이라고 부른다.[3] 예를 들면, 용암, 깨진 유리 등이 있다.[4] 보통 '킬파트'라고 한다.

분류