Menu
CoddyTech

ブロック

Coddyの「Robloxゲーム:バトルグラウンド」コースのレッスン 12/25。

Fブロック:Settings.BlockTime秒間、受ける各攻撃のダメージは、その一部であるSettings.BlockMultiplierだけになります。ブロック中かどうかはキャラクターの属性Blockingなので、どんなものからでも読み取れます。

すべての攻撃はhurtを通るため、このルールは一度だけ記述します。

if character:GetAttribute("Blocking") then
	amount *= Settings.BlockMultiplier
end

チャプター4ではボットが攻撃してきますが、同じ行がそのときもあなたを守ってくれます。

challenge icon

チャレンジ

中級

Fキーでブロック:しばらくの間、受ける攻撃は5分の1になる。

  1. abilities.Block(player, character)を追加します。キャラクターのBlocking属性をtrueに設定し、Settings.BlockTimeの時間が経過した後、キャラクターがまだ存在していれば、その属性をクリアします。
  2. hurtでは、キャラクターがBlocking状態の間、amountにSettings.BlockMultiplierを掛けます。
  3. BattleClientで"Block"をFキーに割り当て、shownに追加します。

「プレイ」を押してからFキーを押します。「Block」の表示がカウントダウンします。その後、「確認」を押します。

自分で試してみよう

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local remotes = game.ReplicatedStorage.Remotes
local lastSwing = {} -- player -> 最後に剣を振った時刻

-- Roblox標準の剣のアニメーション
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"

print("Swords deal " .. Settings.SwordDamage .. " damage")

-- ゲーム内のすべての攻撃はここを通る
local function hurt(humanoid, amount)
	if humanoid.Health <= 0 then
		return
	end
	local character = humanoid.Parent
	humanoid:TakeDamage(amount)
end

-- 剣を振る処理:アニメーションの後、前方の判定範囲内にいる全員に攻撃が当たる
local function swing(player)
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or character.Humanoid.Health <= 0 then
		return
	end
	local now = time()
	if lastSwing[player] and now - lastSwing[player] < Settings.SwingCooldown then
		return
	end
	lastSwing[player] = now
	character.Humanoid.Animator:LoadAnimation(slash):Play()
	-- 前方の判定範囲。自分の体は含めない
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local box = root.CFrame * CFrame.new(0, 0, -3)
	local hit = {} -- 剣を振るたびに、各humanoidへの攻撃は1回だけ
	for _, part in workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params) do
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid and not hit[humanoid] then
			hit[humanoid] = true
			hurt(humanoid, Settings.SwordDamage)
		end
	end
end

-- 名前ごとのアビリティと、それぞれの処理
local abilities = {}

-- プレイヤーのキャラクターは本人のデバイスで動かすため、ダッシュもそこで行う
function abilities.Dash(player)
	remotes.Ability:FireClient(player, "Dash")
end

-- 周囲の全員に攻撃が当たり、自分から離れる方向に吹き飛ばされる
function abilities.Slam(player, character, root)
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local hit = {}
	for _, part in workspace:GetPartBoundsInRadius(root.Position, Settings.SlamRadius, params) do
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		local theirRoot = part.Parent:FindFirstChild("HumanoidRootPart")
		if humanoid and theirRoot and not hit[humanoid] then
			hit[humanoid] = true
			hurt(humanoid, Settings.SlamDamage)
			local away = (theirRoot.Position - root.Position) * Vector3.new(1, 0, 1)
			if away.Magnitude > 0 then
				away = away.Unit
			end
			theirRoot:ApplyImpulse((away * Settings.SlamKnockback + Vector3.new(0, 40, 0)) * theirRoot.AssemblyMass)
		end
	end
end

-- レッスン12:Blockはここに追加

-- クライアントは名前でアビリティを要求する。サーバーは、それが実在する
-- アビリティで、プレイヤーが生存し、クールダウンが終わっているかを確認する。再び使用可能になる時刻は
-- プレイヤーの属性として保存されるので、画面に表示できます。
remotes.Ability.OnServerEvent:Connect(function(player, name)
	if typeof(name) ~= "string" or not abilities[name] then
		return
	end
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or character.Humanoid.Health <= 0 then
		return
	end
	local now = workspace:GetServerTimeNow()
	if now < (player:GetAttribute(name .. "ReadyAt") or 0) then
		return
	end
	player:SetAttribute(name .. "ReadyAt", now + Settings.Cooldowns[name])
	abilities[name](player, character, root)
end)

-- すべてのプレイヤーは、復活するたびに剣を受け取ります
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local sword = ServerStorage.Sword:Clone()
		sword.Parent = player.Backpack
		sword.Activated:Connect(function()
			swing(player)
		end)
	end)
end)

-- 訓練用ダミー:体力バーは体力に応じて変化し、倒された
-- ダミーは少しすると同じ場所に新しい個体として復活します
local function setupDummy(dummy)
	local humanoid = dummy.Humanoid
	humanoid.HealthChanged:Connect(function(health)
		dummy.HealthBar.Back.Fill.Size = UDim2.fromScale(health / humanoid.MaxHealth, 1)
	end)
	humanoid.Died:Connect(function()
		local spot = dummy:GetPivot()
		task.wait(0.5)
		dummy:Destroy()
		task.wait(Settings.DummyRespawn)
		local fresh = ServerStorage.Dummy:Clone()
		fresh:PivotTo(spot)
		fresh.Parent = workspace
		setupDummy(fresh)
	end)
end
setupDummy(workspace.Dummy)

Robloxゲーム:バトルグラウンドのすべてのレッスン

自分で練習してみよう: Robloxプレイグラウンド