Menu
CoddyTech

タワーを配置する

Coddyの「Robloxゲーム:タワーディフェンス」コースのレッスン 8/22。

灰色のパッドのいずれかをクリックすると、選んだタワーがその場所に建てられます。クライアントはマウスからクリックを読み取ります。me:GetMouse()、そのButton1Downイベント、そしてポインターの下にあるパーツであるTargetです。しかし、クライアント自身では何も建てられません。タワーはサーバー上にあるからです。クライアントはRemoteEvent Remotes.Placeを通じて要求します。

-- クライアント
remotes.Place:FireServer(workspace.Pads.Pad4, "Frost")
-- サーバー: Roblox が最初にプレイヤーを追加する
remotes.Place.OnServerEvent:Connect(function(player, pad, kind)
end)

タワーモデルはServerStorage.Towers内で待機しています。サーバーはそのうちの1つをパッドの上に複製し、パッドの高さの半分に1を足した分だけ上げて、パッドの上に立つようにします。

challenge icon

チャレンジ

中級

クリックしたパッドに、選択したタワーを建てます。

  1. DefenseClientで、remotes(WaitForChildを使います)と、LocalPlayerであるmeを取得します。
  2. me:GetMouse().Button1Downで、mouse.Targetがworkspace.Padsにあり、selectedが設定されている場合、remotes.Place:FireServer(target, selected)を呼び出します。
  3. DefenseServerでは、remotes.Place.OnServerEventがplaceTower(pad, kind)を呼び出します。ServerStorage.Towers[kind]を複製して、パッド上のworkspace.Towersに配置します。

「プレイ」を押し、Archerを選んで灰色のパッドをクリックすると、Archerがその上に立ちます。その後、「確認」を押します。

自分で試してみよう

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プレイグラウンド