Menu
CoddyTech

클릭해서 부수기

Coddy의 Roblox 게임: 펫 시뮬레이터 코스 레슨. 18개 중 6번째.

ClickDetector는 버튼에 적합하지만, 대부분의 시뮬레이터는 클라이언트에서 클릭을 읽으므로 서버가 응답하기 전에 클릭하는 순간 게임이 반응할 수 있습니다(소리, 흔들림). 클라이언트의 마우스는 무엇을 클릭했는지 알고 있습니다:

local mouse = me:GetMouse()
mouse.Button1Down:Connect(function()
	print(mouse.Target) -- 클릭한 위치에 있는 파트, 또는 nil
end)

클라이언트는 더미를 직접 부술 수 없습니다. 더미와 코인은 서버가 소유합니다. 클라이언트는 클라이언트와 서버 간의 메시지 전달을 위해 만들어진 객체인 RemoteEvent를 통해 서버에 요청합니다:

-- 클라이언트
remotes.Hit:FireServer(pile)
-- 서버: Roblox는 이를 보낸 플레이어를 첫 번째 인자로 추가합니다
remotes.Hit.OnServerEvent:Connect(function(player, pile)
end)

네트워크를 거치므로 RemoteEvent는 잠시 후에 도착합니다. 휴대폰에서는 탭이 클릭과 같습니다.

challenge icon

챌린지

중급
  1. PetClient에서: local remotes = game.ReplicatedStorage:WaitForChild("Remotes"). 마우스의 Button1Down 이벤트에서 mouse.Target이 CoinPile이라는 이름의 파트라면, 해당 파트를 인수로 remotes.Hit을 실행합니다.
  2. PetServer에서: local remotes = game.ReplicatedStorage.Remotes를 작성하고, remotes.Hit.OnServerEvent를 연결해 hitPile(player, pile)을 호출합니다. 그리고 spawnPile에서 ClickDetector를 제거합니다(또한 local hitPile 줄을 제거하고, 대신 spawnPile 아래에 local function hitPile을 작성합니다).

Play를 누르고 더미를 클릭합니다. 그런 다음 Check를 누릅니다.

직접 해보기

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local piles = workspace.Piles
local pileSpot = {} -- 각 더미 -> 더미가 놓인 자리

print("A Meadow pile gives " .. Settings.PileValue .. " coins")

-- 모든 플레이어는 리더보드에 Coins를 갖게 됩니다
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
end)

-- 더미 클릭 처리에는 spawnPile 아래에 작성된 hitPile이 필요하므로, 이름을
-- 여기서 둘보다 먼저 선언합니다
local hitPile

-- 자리에 놓이는 코인 더미: ServerStorage.CoinPile의 복사본으로, 체력
-- 과 가치를 속성으로 갖습니다
local function spawnPile(spot)
	local pile = ServerStorage.CoinPile:Clone()
	pile.Position = spot.Position
	pile:SetAttribute("MaxHealth", Settings.PileHealth)
	pile:SetAttribute("Health", Settings.PileHealth)
	pile:SetAttribute("Value", Settings.PileValue)
	-- 클릭해서 더미를 타격합니다
	local detector = Instance.new("ClickDetector")
	detector.Parent = pile
	pileSpot[pile] = spot
	pile.Parent = piles
	detector.MouseClick:Connect(function(player)
		hitPile(player, pile)
	end)
	return pile
end

-- 타격할 때마다 더미의 체력이 1 감소합니다;
-- 체력이 0이 되면 더미가 부서지고 해당 가치만큼 보상을 줍니다
function hitPile(player, pile)
	local health = pile:GetAttribute("Health") - 1
	if health > 0 then
		pile:SetAttribute("Health", health)
		return
	end
	player.leaderstats.Coins.Value += pile:GetAttribute("Value")
	local spot = pileSpot[pile]
	pileSpot[pile] = nil
	pile:Destroy()
	-- 몇 초 후 같은 자리에 새 더미가 생깁니다
	task.delay(Settings.RespawnTime, spawnPile, spot)
end

-- Meadow의 모든 자리에 더미를 하나씩 놓습니다
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
	spawnPile(spot)
end
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

Roblox 게임: 펫 시뮬레이터의 모든 레슨