Menu
CoddyTech

펫이 따라와요

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

펫은 플레이어를 따라다녀야 합니다. 밖에 나와 있는 펫이 Settings.MaxEquipped(3)마리보다 적으면 새 펫이 즉시 나옵니다. 나온 펫에는 Equipped 특성이 표시됩니다.

밖에 나와 있는 각 펫은 월드에 파트 하나를 갖습니다. 이 파트는 ServerStorage.PetModels에 있는 펫 모델의 복사본이며, 서버는 매 프레임마다 이 파트를 주인 뒤에 배치합니다. RunService.Heartbeat는 매 프레임 한 번 실행되고, CFrame을 곱하면 하나를 다른 하나에 대해 상대적으로 배치할 수 있습니다.

model.CFrame = root.CFrame * CFrame.new(0, -1.5, 4) -- 뒤로 4스터드, 약간 아래로

테이블은 각 파트가 어떤 펫에 속하는지 기억하므로, 펫을 집어넣으면 해당 파트도 사라집니다.

challenge icon

챌린지

중급
  1. equippedCount(player)를 작성하세요. givePet에 세 번째 인수 equipped를 추가하고, 이를 Equipped 속성으로 저장한 다음 equippedCount(player) < Settings.MaxEquipped로 부화시키세요.
  2. RunService와 local petParts = workspace.PetParts를 가져오세요. Heartbeat에서 각 플레이어와 각 펫에 대해, 펫이 나와 있고 플레이어에게 캐릭터가 있으면 펫에 파트가 있는지 확인하세요(ServerStorage.PetModels[pet.Value]의 복제본을 petParts에 넣음). 그리고 root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4) 위치에 배치하면서 슬롯을 1, 2, 3으로 셉니다. 펫을 집어넣으면 해당 파트를 제거하세요.

Play를 누르고, 부화한 다음 걸어 보세요. 그런 다음 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")

-- 이름별 Settings.Pets
local petInfo = {}
for _, info in Settings.Pets do
	petInfo[info.Name] = info
end

-- 펫: 플레이어의 Pets 폴더에 있는 StringValue로, 펫의 이름을 저장하고
-- 희귀도와 힘을 속성으로 가짐
local function givePet(player, name)
	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.Parent = player.Pets
	return pet
end

-- 모든 플레이어에게 리더보드의 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)

-- 자리에 놓인 코인 더미: 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)

-- 가중치 추첨: 각 펫의 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)
	print(player.Name .. " hatched a " .. name)
end)
quiz icon실력 점검

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

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