알 부화시키기
Coddy의 Roblox 게임: 펫 시뮬레이터 코스 레슨. 18개 중 9번째.
스폰 지점 근처의 흰색 알에는 ProximityPrompt가 있습니다. 알 가까이 다가가면 Roblox에 누르고 있어야 하는 키와 함께 "부화"가 표시됩니다(E 키 또는 휴대폰에서 탭). 플레이어가 충분히 오래 누르고 있으면 서버에서 플레이어와 함께 프롬프트의 Triggered 이벤트가 발생합니다.
workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
end)서버는 가격을 확인하고, 코인을 차감한 뒤 펫을 줍니다. 펫은 이름을 저장하는 StringValue이며, 플레이어 내부의 Pets 폴더에 들어 있습니다. 서버가 플레이어 내부에 넣는 모든 항목은 해당 플레이어의 클라이언트로 복제되므로, 이후 강의의 펫 목록은 무엇을 표시할지 알 수 있습니다.
챌린지
중급- 플레이어가 참가하면
Pets라는 이름의 Folder도 제공합니다. givePet(player, name)을 작성합니다. 플레이어의 Pets 안에 이름이Pet이고 Value가 name인 StringValue를 만들고, 반환합니다.workspace.Egg.ProximityPrompt.Triggered를 연결합니다. 플레이어의 코인이Settings.EggPrice보다 적으면 중단하고, 그렇지 않으면 가격만큼 차감한 뒤Cat을 주고Player1 hatched a Cat을 출력합니다(플레이어의 Name 사용).
Play를 누르고 코인 25개를 모은 다음, 알에서 E 키를 길게 누르세요. 그런 다음 Check를 누르세요.
직접 해보기
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
-- 클라이언트가 더미를 타격하겠다고 요청합니다. 클라이언트를 절대 신뢰하지 마세요: 누구나
-- 어디서든 원하는 만큼 빠르게 임의의 값으로 이 remote를 호출할 수 있습니다.
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)
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.