Menu
CoddyTech

알 부화시키기

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

스폰 지점 근처의 흰색 알에는 ProximityPrompt가 있습니다. 알 가까이 다가가면 Roblox에 누르고 있어야 하는 키와 함께 "부화"가 표시됩니다(E 키 또는 휴대폰에서 탭). 플레이어가 충분히 오래 누르고 있으면 서버에서 플레이어와 함께 프롬프트의 Triggered 이벤트가 발생합니다.

workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
end)

서버는 가격을 확인하고, 코인을 차감한 뒤 펫을 줍니다. 펫은 이름을 저장하는 StringValue이며, 플레이어 내부의 Pets 폴더에 들어 있습니다. 서버가 플레이어 내부에 넣는 모든 항목은 해당 플레이어의 클라이언트로 복제되므로, 이후 강의의 펫 목록은 무엇을 표시할지 알 수 있습니다.

challenge icon

챌린지

중급

알에서 코인 25개를 내고 고양이를 부화시키세요.

  1. PlayerAdded에서 플레이어에게 Pets라는 Folder를 추가하세요.
  2. givePet(player, name)을 작성하세요. Pets 안에 Value가 name인 Pet StringValue를 만들고 반환하세요.
  3. workspace.Egg.ProximityPrompt.Triggered에서 Coins가 Settings.EggPrice 이상이면 해당 코인을 차감하고 givePet(player, "Cat")을 호출한 다음 player.Name .. " hatched a Cat"을 출력하세요.

‘플레이’를 누르고 코인 25개를 모은 뒤, 알 앞에서 E를 길게 누르세요. Coins가 25 감소합니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

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

local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local pileSpot = {} -- 각 더미 -> 그 더미가 놓인 위치
local lastHit = {} -- 각 플레이어 -> 마지막으로 타격한 시각

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)

-- 위치에 놓인 코인 더미: 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)
	pile.HealthGui.Label.Text = pile:GetAttribute("Health") .. "/" .. pile:GetAttribute("MaxHealth")
	pileSpot[pile] = spot
	pile.Parent = piles
	return pile
end

-- 타격할 때마다 더미의 체력이 1 감소합니다;
-- 체력이 0이 되면 더미가 부서지고 해당 값만큼 지급합니다
local function hitPile(player, pile)
	local health = pile:GetAttribute("Health") - 1
	if health > 0 then
		pile:SetAttribute("Health", health)
		pile.HealthGui.Label.Text = health .. "/" .. pile:GetAttribute("MaxHealth")
		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

-- 클라이언트가 더미를 타격하겠다고 요청합니다. 클라이언트를 절대 신뢰하지 마세요. 누구나
-- 어디서든 원하는 만큼 빠르게 어떤 값이든 담아 이 리모트를 호출할 수 있습니다.
remotes.Hit.OnServerEvent:Connect(function(player, pile)
	-- 실제 더미인지 확인
	if typeof(pile) ~= "Instance" or pile.Parent ~= piles then
		return
	end
	-- 닿을 수 있는 거리 내에 있는지
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or (root.Position - pile.Position).Magnitude > Settings.Reach then
		return
	end
	-- 너무 빠르게 연속으로 공격하지 않는지
	local now = time()
	if lastHit[player] and now - lastHit[player] < Settings.HitCooldown then
		return
	end
	lastHit[player] = now
	hitPile(player, pile)
end)

-- 여기에 레슨 9: 알 부화시키기 코드가 들어갑니다

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

직접 연습해 보세요: Roblox 플레이그라운드