Service, Part, Player 등 객체의 유형에 따라 다양한 방식으로 필요한 객체를 가져올 수 있습니다.
Service 객체 가져오기
Service 객체란 게임 내에서 특정한 기능을 수행하도록 설계된 기본 제공 시스템 객체를 의미합니다.
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local StarterGui = game:GetService("StarterGui")
local StarterPlayer = game:GetService("StarterPlayer")
local Players = game:GetService("Players")
...
부모 객체 참조
-- 객체의 부모
local ParentPart = Part.Parent
-- 스크립트의 부모
local ScriptParent = script.Parent
자식 객체 참조
-- 주소값으로 객체 반환 (객체가 로드되기 전이면 실패할 수 있습니다.)
local ChildPart1 = Workspace.ChildPart
-- 이름에 해당하는 첫번째 객체 반환
local ChildPart2 = Workspace:FindFirstChild("ChildPart")
-- 객체가 반환될때까지 대기
local ChildPart3 = SomePart:WaitForChild("ChildPart")
모든 자식 가져오기
Part의 계층 구조가 아래와 같을 때
local Part = Workspace:WaitForChild("Part")
-- 부모 객체(Part)의 모든 첫번째 자식 객체를 반환
local Children = Part:GetChildren()
for _, child in ipairs(Children) do
-- ChildPart1, ChildPart2 출력
print(child.Name)
end
-- 부모 객체(Part)의 모든 하위 자식 객체를 반환
local Descendants = Part:GetDescendants()
for _, descendant in ipairs(Descendants) do
-- ChildPart1, ChildPart1-1, ChildPart2, ChildPart2-1 모두 출력
print(descendant.Name)
end
모든 플레이어 가져오기
local Players = game:GetService("Players")
local PlayerList = Players:GetPlayers()
for i = 1, #PlayerList do
print(PlayerList[i])
end
LocalPlayer 가져오기 (LocalScript)
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer