クールダウンの表示
Coddyの「Robloxゲーム:バトルグラウンド」コースのレッスン 8/20。
プレイヤーには、アビリティが使用可能になったタイミングが分かるようにする必要があります。HUDには、各アビリティ用のラベルが付いたCooldownsフレームがあり、サーバーはすでにプレイヤーのDashReadyAtを保持していて、クライアントから読み取れます。
カウントダウンは毎フレーム変化するため、クライアントはフレームごとに1回発火するRunService.Heartbeatで表示を更新します。string.formatを使うと、小数第1位まで表示できます。
string.format("%s %.1f", "Dash", 2.345) --> "Dash 2.3"クライアントはクールダウンを表示するだけです。使用可否は、前のレッスンで扱ったサーバー側のチェックによって引き続き決まります。
チャレンジ
中級BattleClientでRunServiceを取得し、local me = Players.LocalPlayerとlocal hud = script.Parentを設定します。各Heartbeatで、local shown = { "Dash" }内の各nameについて、クールダウン中はhud.Cooldowns[name].TextをDash 2.3に、使用可能になったらDash readyに設定します。
Playを押し、Qを押してラベルを確認します。その後、Checkを押します。
自分で試してみよう
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 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
-- 名前ごとに定義されたアビリティと、その動作
local abilities = {}
function abilities.Dash(player, character, root)
root.AssemblyLinearVelocity = root.CFrame.LookVector * Settings.DashSpeed + Vector3.new(0, 15, 0)
end
-- クライアントは名前を指定してアビリティを要求する。サーバーは、実在する
-- アビリティであること、プレイヤーが生存していること、クールダウンが終わっていることを確認する。次に使用可能になる時刻は
-- プレイヤーの属性として保存し、画面に表示できるようにする。
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)
-- すべてのプレイヤーにリーダーボードの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)
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。