로블록스 게임제작

UI 상호작용 및 커스텀 이벤트 전달 (local -> server)

Dean83 2024. 2. 13. 13:12

1. UI 추가 방법

  • StarterGUI -> ScreenGUI  안에 UI를 추가 할 수 있다.
  • 해당 UI는 화면에 항상 고정되어 나타난다. 
  • 혹은 메뉴 -> home -> UI 에서 추가 가능하다. 

2. UI 상호작용

  • 버튼 상호작용 예 (Local Script)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local playerGui = game.Players.LocalPlayer.PlayerGui
local screenGui = playerGui:FindFirstChild("ScreenGui")
local uis = screenGui:GetChildren()

for _,item in pairs(uis) do
	if item.Name == 'UI이름' and item.ClassName == 'UI타입' then
		item.Activated:Connect(function()
        	--아래 코드는 서버 스크립트로 이벤트를 보내는 코드
			local remoteEvent = game.ReplicatedStorage.RemoteEvent
			remoteEvent:FireServer(item.Name)
		end)
	end
	
	
end

 

3. 서버 스크립트 이벤트 수신

local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "RemoteEvent"
remoteEvent.Parent = game.ReplicatedStorage

-- 클라이언트로부터 이벤트 수신
remoteEvent.OnServerEvent:Connect(function(player,name)
	
	print(name)
	-- 캐릭터와 버튼 이름 수신. 필요할시 인자값을 더 늘리면 된다.
end)