ショップ
Coddyの「Robloxゲーム:タワーディフェンス」コースのレッスン 7/22。
次はプレイヤー側です。DefenseClientはHUD内にあるLocalScriptです。HUDはRobloxが各プレイヤーの画面にコピーするScreenGuiなので、script.ParentはHUDを指します。そのShopフレームには、各タワーにつき1つのTextButtonがあり、タワーの名前が付けられています。
ゲームのオブジェクトがプレイヤーのデバイスに届く前にLocalScriptが実行されることがあるため、WaitForChildを使ってオブジェクトを取得します。ボタンのActivatedイベントは、クリックまたはタップされたときに発生します。
local Settings = require(game.ReplicatedStorage:WaitForChild("Settings"))
local button = script.Parent.Shop.Frost
button.Text = "Frost tower"
button.Activated:Connect(function()
print("Frost picked")
end)チャレンジ
中級DefenseClientでショップのボタンに価格を表示します。
local Settings = require(game.ReplicatedStorage:WaitForChild("Settings"))とlocal hud = script.Parentを追加します。hud.Shopにある各TextButtonについて、テキストをbutton.Name .. " $" .. Settings.Towers[button.Name].Priceに設定します。- その
Activated時に、名前をlocal selectedに保存します。
「プレイ」を押します。ショップにArcher $60、Cannon $120、Frost $90と表示されます。その後、「確認」を押します。
自分で試してみよう
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local enemies = workspace.Enemies
print("The base can take " .. Settings.BaseHealth .. " hits")
-- 順番に並んだウェイポイント(名前は 1, 2, 3...)
local waypoints = workspace.Path.Waypoints:GetChildren()
table.sort(waypoints, function(a, b)
return tonumber(a.Name) < tonumber(b.Name)
end)
-- workspace の属性として設定した拠点の体力:すべてのクライアントが読み取れる
workspace:SetAttribute("BaseHealth", Settings.BaseHealth)
-- すべてのプレイヤーにリーダーボード上の Money が付与される
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = Settings.StartMoney
money.Parent = leaderstats
end)
-- 敵が拠点に到達した
local function damageBase(amount)
local health = math.max(0, workspace:GetAttribute("BaseHealth") - amount)
workspace:SetAttribute("BaseHealth", health)
print("Base: " .. health)
end
-- 敵を経路に沿って移動させる。MoveTo は 8 秒後に移動を諦め、そのことを通知する
-- (MoveToFinished が false になる)。そのため、遅い敵には到着するまで再び移動を指示する。
local function walkPath(enemy)
local humanoid = enemy.Humanoid
for i = 2, #waypoints do
repeat
humanoid:MoveTo(waypoints[i].Position)
until humanoid.MoveToFinished:Wait() or not enemy.Parent
if not enemy.Parent then
return
end
end
-- 拠点に到達した
damageBase(Settings.Enemies[enemy.Name].Damage)
enemy:Destroy()
end
-- ゲートに現れ、経路を進む新しい敵
local function spawnEnemy(kind)
local enemy = ServerStorage.Enemies[kind]:Clone()
-- 最初のウェイポイントのすぐ上に配置する
enemy:PivotTo(waypoints[1].CFrame + Vector3.new(0, 5, 0))
local info = Settings.Enemies[kind]
enemy.Humanoid.MaxHealth = info.Health
enemy.Humanoid.Health = info.Health
enemy.Humanoid.WalkSpeed = info.Speed
enemy.Parent = enemies
task.spawn(walkPath, enemy)
return enemy
end
-- 1 つのウェーブ:敵を 1 体ずつ出し、全員いなくなるまで待つ
local function runWave(list)
for _, kind in list do
spawnEnemy(kind)
task.wait(Settings.SpawnGap)
end
while #enemies:GetChildren() > 0 do
task.wait(0.2)
end
end
-- ウェーブはそれぞれ休憩を挟んで始まる
for i, list in Settings.Waves do
task.wait(Settings.WaveBreak)
runWave(list)
end
Robloxゲーム:タワーディフェンスのすべてのレッスン
自分で練習してみよう: Robloxプレイグラウンド