Menu
CoddyTech

ペットを3匹出す

Coddyの「Robloxゲーム:ペットシミュレーター」コースのレッスン 12/20。

ペットは一列に並び、一度に外に出られるのは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の場合、slot 1は5 studs左に、slot 3は5右に移動します。

challenge icon

チャレンジ

中級

最大3匹のペットを、後ろに横一列に並べましょう。

  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) の位置に配置しましょう。

「プレイ」を押して4匹孵化させましょう。3匹のペットが横並びで後ろを歩きます。その後、「確認」を押しましょう。

自分で試してみよう

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 -> それが置かれている 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

-- レッスン 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プレイグラウンド