Menu
CoddyTech

ペット一覧

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

プレイヤーには自分のペットが見える必要があります。HUDにはInventoryというScrollingFrameがあり、その中にUIListLayoutがあります。中に入れたものをLayoutOrderに従って上から順に並べます。各ペットにはここにTextButtonが作成されます。AutomaticCanvasSizeはYなので、リストに合わせてキャンバスが大きくなり、ペットをたくさん持つプレイヤーはスクロールして閲覧できます。

クライアントは、サーバーがプレイヤー内に保持するPetsフォルダーからリストを作成し、変更があったときに再作成します。ペットが孵化したときのChildAddedと、ペットを装備したり外したりしたときの各ペットのGetAttributeChangedSignal("Equipped")です。

pet:GetAttributeChangedSignal("Equipped"):Connect(showPets)

再作成の際は、まず古いボタンを削除します。これにより、リストに同じペットが二重に表示されることはありません。

challenge icon

チャレンジ

中級

PetClient内で:

  1. プレイヤー内のPetsを待ちます。
  2. showPets()を記述します。hud.Inventory内のTextButtonsを破棄し、ペットごとに1つ追加します(LayoutOrderは順番、幅はUDim2.new(1, -12, 0, 30)、白色でサイズ調整されたテキスト)。出ているペットにはCat (out)、出ていないペットにはCatと表示し、出ているときは緑色にします。
  3. 今すぐ、ペットが追加されたとき、そしていずれかのペットのEquippedが変わったときに呼び出します。

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 = {} -- 各コインの山 -> その山が置かれている場所
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, 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

-- プレイヤーが出しているペットの数
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

-- プレイヤーのペットが各打撃に加えるパワー
local function petPower(player)
	local power = 0
	for _, pet in player.Pets:GetChildren() do
		if pet:GetAttribute("Equipped") then
			power += pet:GetAttribute("Power")
		end
	end
	return power
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回叩くと、コインの山の体力が1とプレイヤーのペットのパワーの合計だけ減る。
-- 0になるとコインの山が壊れ、その価値に応じたコインが支払われる
local function hitPile(player, pile)
	local health = pile:GetAttribute("Health") - (1 + petPower(player))
	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()
	-- 空きがあれば、すぐに連れ出す
	local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
	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")
		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)
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

Robloxゲーム:ペットシミュレーターのすべてのレッスン