Menu
CoddyTech

펫 세 마리 꺼내기

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

펫은 줄을 서야 하며, 한 번에 밖에 나와 있을 수 있는 펫은 Settings.MaxEquipped(3)마리뿐입니다. 새 펫은 밖에 나와 있는 펫이 그보다 적을 때만 나오고, 나머지는 Pets 폴더에서 기다립니다.

밖에 나와 있는 펫의 수를 세는 방법은 폴더를 반복해서 확인하는 것입니다.

local count = 0
for _, pet in player.Pets:GetChildren() do
	if pet:GetAttribute("Equipped") then
		count += 1
	end
end

펫을 한 줄로 세우려면 배치하면서 밖에 나와 있는 펫에 1, 2, 3과 같이 번호를 매기고, 그 번호를 좌우 오프셋으로 바꿉니다. (slot - 2) * 5를 사용하면 슬롯 1은 왼쪽으로 5 studs, 슬롯 3은 오른쪽으로 5 studs 이동합니다.

challenge icon

챌린지

중급

최대 세 마리의 펫을 네 뒤에 일렬로 세우세요.

  1. 장착한 플레이어의 펫 수를 세는 equippedCount(player)를 작성하세요.
  2. givePet(player, name, equippedCount(player) < Settings.MaxEquipped)로 부화시키세요.
  3. Heartbeat에서 밖에 나와 있는 펫을 순회하며 slot을 1, 2, 3으로 세고, 각각을 root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4) 위치에 배치하세요.

‘플레이’를 누르고 알을 네 번 부화시키세요. 펫 세 마리가 나란히 네 뒤를 따라 걷습니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

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

-- 펫: 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

-- Lesson 12: Three Pets Out을 여기에 추가

-- 모든 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

	-- 소유한 펫들
	local pets = Instance.new("Folder")
	pets.Name = "Pets"
	pets.Parent = player
end)

-- spot에 놓인 코인 더미: 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()
	-- 몇 초 후 같은 자리에 새 더미 생성
	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)

-- 가중치 추첨: 각 펫의 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

-- 부화: 달걀의 프롬프트로 진행하며 코인으로 비용을 지불합니다
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()
	-- 즉시 꺼내 주인을 따라다니게 함
	givePet(player, name, true)
	print(player.Name .. " hatched a " .. name)
end)

-- 꺼낸 펫은 주인을 따라다닙니다. 각 펫의 파트가 월드에 생성되어
-- 매 프레임 플레이어 뒤에 유지됩니다
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")
		for _, pet in pets and pets:GetChildren() or {} do
			local model = petModels[pet]
			if pet:GetAttribute("Equipped") and root then
				if not model then
					model = ServerStorage.PetModels[pet.Value]:Clone()
					model.Parent = petParts
					petModels[pet] = model
				end
				model.CFrame = root.CFrame * CFrame.new(0, -1.5, 4)
			end
		end
	end
end)

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

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