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