Menu
CoddyTech

Qキーでダッシュ

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

アビリティはプレイヤーのデバイス上で開始します。BattleClientはHUD内のLocalScriptで、ContextActionServiceはアクションをキーに割り当てます。また、3番目の引数にtrueを指定すると、スマートフォンの画面にもそのアクション用のボタンが表示されます。

ContextActionService:BindAction("Dash", use, true, Enum.KeyCode.Q)

関数はアクションの名前と入力の状態を受け取ります。キーが押されたときの状態はBeginです。ゲームのルールはサーバーが管理しているため、クライアントが独自にダッシュすることはありません。名前を指定して、RemoteEvent Remotes.Abilityを通じてサーバーに要求します。サーバーは名前ごとにアビリティのテーブルを保持します。ダッシュは押し出す動作です。rootの速度を設定すると、キャラクターが前方に飛び出します。

function abilities.Dash(player, character, root)
	root.AssemblyLinearVelocity = root.CFrame.LookVector * Settings.DashSpeed + Vector3.new(0, 15, 0)
end
challenge icon

チャレンジ

中級

BattleClient内で、ContextActionServiceとRemotesフォルダーを取得し、Begin時に名前を指定してremotes.Abilityを発火するuse(name, state)を書き、タッチボタン付きでQに"Dash"をバインドします。

BattleServer内で、local remotes = game.ReplicatedStorage.Remotesを追加し、Dashを含むabilitiesテーブルを作成して、remotes.Ability.OnServerEvent(player, name)を接続し、指定されたアビリティをプレイヤー、そのキャラクター、およびそのルートとともに実行します。

プレイを押して、Qを押します。その後、確認を押します。

自分で試してみよう

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

local lastSwing = {} -- player -> 最後に剣を振った時刻

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

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

-- 剣を振る処理:アニメーションの後、前方のボックス内にいる全員に攻撃が当たる
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
			humanoid:TakeDamage(Settings.SwordDamage)
		end
	end
end

-- すべてのプレイヤーにリーダーボードのKillsと、復活するたびに剣を与える
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats

	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)
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

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