卵を孵化させる
Coddyの「Robloxゲーム:ペットシミュレーター」コースのレッスン 9/20。
スポーン地点の近くにある白い卵にはProximityPromptが付いています。近づくと、Robloxに長押しするキー(E、またはスマートフォンでのタップ)とともに「孵化」と表示されます。プレイヤーが十分な時間長押しすると、サーバー上でプレイヤーとともにプロンプトのTriggeredが発火します。
workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
end)サーバーは価格を確認し、コインを受け取り、ペットを付与します。ペットは名前を保持するStringValueで、プレイヤー内のPetsフォルダーにあります。サーバーがプレイヤー内に配置したものはすべてそのプレイヤーのクライアントに複製されるため、後のレッスンでペットのリストに何を表示するかが分かります。
チャレンジ
中級卵のところで25コインを使って猫を孵化させましょう。
PlayerAddedで、プレイヤーにPetsという名前のFolderを追加します。givePet(player, name)を記述します。PetsにValueがnameのStringValuePetを作成し、それを返します。workspace.Egg.ProximityPrompt.Triggeredで、Coinsが少なくともSettings.EggPriceある場合はその額を差し引き、givePet(player, "Cat")を呼び出し、player.Name .. " hatched a Cat"を出力します。
「プレイ」を押して、コインを25枚獲得し、卵のところでEキーを長押しします。Coinsが25減ります。その後、「確認」を押します。
自分で試してみよう
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")
-- すべてのプレイヤーにリーダーボード上の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
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)
-- レッスン 9: 卵を孵化させる処理はここに入ります
Robloxゲーム:ペットシミュレーターのすべてのレッスン
自分で練習してみよう: Robloxプレイグラウンド