펫은 더 빨리 파요
Coddy의 Roblox 게임: 펫 시뮬레이터 코스 레슨. 18개 중 12번째.
펫 시뮬레이터에서 펫들은 땅을 팝니다. 나와 있는 각 펫은 타격할 때마다 자신의 Power를 더합니다. 여우(파워 4)가 있으면 타격당 1 대신 5의 피해를 줍니다.
나와 있는 펫들의 파워를 모두 더하고, 그 값에 1을 더해 더미의 체력에서 빼세요. 집어넣은 펫은 도움이 되지 않습니다.
챌린지
중급petPower(player)를 작성하세요: 밖에 나와 있는 플레이어의 펫들이 가진 Power의 합입니다.hitPile에서 체력에서 1 대신1 + petPower(player)를 빼세요.
이 레슨의 더미는 체력이 8입니다. Play를 누른 다음 Check를 누르세요.
직접 해보기
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local petParts = workspace.PetParts
local pileSpot = {} -- 각 pile -> 해당 pile이 놓인 spot
local lastHit = {} -- 각 player -> 마지막으로 공격한 시각
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- 이름을 기준으로 정리한 Settings.Pets
local petInfo = {}
for _, info in Settings.Pets do
petInfo[info.Name] = info
end
-- pet: player의 Pets 폴더에 있는 StringValue로, 이름을 저장하고
-- 희귀도와 파워를 속성으로 가짐
local function givePet(player, name, equipped)
local info = petInfo[name]
local pet = Instance.new("StringValue")
pet.Name = "Pet"
pet.Value = name
pet:SetAttribute("Rarity", info.Rarity)
pet:SetAttribute("Power", info.Power)
pet:SetAttribute("Equipped", equipped)
pet.Parent = player.Pets
return pet
end
-- player가 데리고 있는 pet의 수
local function equippedCount(player)
local count = 0
for _, pet in player.Pets:GetChildren() do
if pet:GetAttribute("Equipped") then
count += 1
end
end
return count
end
-- 모든 player의 리더보드에 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
-- 해당 player가 소유한 pet들
local pets = Instance.new("Folder")
pets.Name = "Pets"
pets.Parent = player
end)
-- spot에 놓인 코인 pile: 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
-- 한 번 공격할 때마다 pile의 체력이 1 감소함;
-- 체력이 0이 되면 pile이 부서지고 그 가치만큼 코인을 지급함
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()
-- 몇 초 뒤 같은 위치에 새 pile 생성
task.delay(Settings.RespawnTime, spawnPile, spot)
end
-- Meadow의 모든 위치에 pile 하나씩 생성
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
spawnPile(spot)
end
-- 클라이언트가 pile을 공격해 달라고 요청한다. 클라이언트를 절대 신뢰하지 말 것. 누구나
-- 어디서든 무엇이든 전달하며 원하는 만큼 빠르게 이 remote를 호출할 수 있다.
remotes.Hit.OnServerEvent:Connect(function(player, pile)
-- 실제 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)
-- 가중치 추첨: 각 pet의 Chance는 전체 100 중 차지하는 비율이다
local function rollPet()
local roll = math.random() * 100
for _, info in Settings.Pets do
roll -= info.Chance
if roll <= 0 then
return info.Name
end
end
return Settings.Pets[1].Name
end
-- 부화: 알의 프롬프트를 사용하며 coins로 비용을 지불한다
workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
local coins = player.leaderstats.Coins
if coins.Value < Settings.EggPrice then
return
end
coins.Value -= Settings.EggPrice
local name = rollPet()
-- 자리가 있으면 즉시 내보낸다
local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
print(player.Name .. " hatched a " .. name)
end)
-- 밖에 나온 Pets는 주인을 따라다닌다. 각각 월드에 part가 있으며, 매 프레임 플레이어
-- 뒤에 한 줄로 배치된다
local petModels = {} -- 각 펫 -> 월드에 있는 해당 펫의 파트
RunService.Heartbeat:Connect(function()
for _, player in Players:GetPlayers() do
local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
local pets = player:FindFirstChild("Pets")
local slot = 0
for _, pet in pets and pets:GetChildren() or {} do
local model = petModels[pet]
if pet:GetAttribute("Equipped") and root then
slot += 1
if not model then
model = ServerStorage.PetModels[pet.Value]:Clone()
model.Parent = petParts
petModels[pet] = model
end
model.CFrame = root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4)
elseif model then
model:Destroy()
petModels[pet] = nil
end
end
end
end)
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.