ペットがついてくる
Coddyの「Robloxゲーム:ペットシミュレーター」コースのレッスン 11/18。
ペットはあなたの周りをついてくるはずです。外に出ているペットが Settings.MaxEquipped(3)未満なら、新しいペットがすぐに出てきて、Equipped 属性が付与されます。
外に出ている各ペットにはワールド内にパーツが割り当てられ、ServerStorage.PetModels からモデルのコピーが作成されます。そしてサーバーは毎フレーム、そのパーツを飼い主の後ろに配置します。RunService.Heartbeat はフレームごとに1回発生し、CFrameを乗算すると、ある位置を別の位置に対する相対位置に配置できます。
model.CFrame = root.CFrame * CFrame.new(0, -1.5, 4) -- 後方に4スタッド、少し低い位置テーブルで各パーツがどのペットに属しているかを記録するため、ペットをしまうと、そのパーツも失われます。
チャレンジ
中級equippedCount(player)を作成します。givePetに3番目の引数equippedを追加し、Equipped属性として保存します。そして、equippedCount(player) < Settings.MaxEquippedで孵化させます。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
-- クライアントが山を叩くよう要求する。クライアントを決して信用しないこと。誰でも
-- どこからでも、何を指定しても、好きな速さでこのリモートイベントを発火できる。
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)
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。