ペットがついてくる
Coddyの「Robloxゲーム:ペットシミュレーター」コースのレッスン 11/20。
ペットはあなたの周りについてくるようにします。孵化したペットはすぐに現れ、Equipped属性が付けられ、ワールド内にパーツが作られます。それはServerStorage.PetModelsにあるモデルのコピーです。
サーバーは毎フレーム、そのパーツを飼い主の後ろに配置します。RunService.Heartbeatは1フレームに1回発生し、CFrameを掛け合わせることで、あるものを別のものに対して相対的に配置できます。
part.CFrame = root.CFrame * CFrame.new(0, 2, 6) -- 後方に6スタッド、上方に2スタッドペットをキーとするテーブルにパーツを記録しておけば、各ペットに1つずつ、パーツを一度だけ作成できます。
if not petModels[pet] then
petModels[pet] = ServerStorage.PetModels[pet.Value]:Clone()
petModels[pet].Parent = petParts
endチャレンジ
中級孵化したペットはあなたについてきます。
givePetに3つ目の引数equippedを追加し、Equipped属性として保存します。孵化時にはgivePet(player, name, true)を呼び出します。RunService、local petParts = workspace.PetParts、local petModels = {}を追加します。RunService.Heartbeatで、装備中の各ペットに対してServerStorage.PetModels[pet.Value]のクローンを1つpetParts内に作成し(petModels[pet]に保持)、そのCFrameをroot.CFrame * CFrame.new(0, -1.5, 4)に設定します。
「プレイ」を押して、ペットを孵化させて歩きましょう。ペットが後をついてきます。その後、「確認」を押します。
自分で試してみよう
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 回叩くと、山の体力が 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)
-- 重み付き抽選:各 pet の 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)
-- レッスン 11:ペットがあなたについてくる処理をここに追加
Robloxゲーム:ペットシミュレーターのすべてのレッスン
自分で練習してみよう: Robloxプレイグラウンド