一振りにつき一度
Coddyの「Robloxゲーム:バトルグラウンド」コースのレッスン 4/25。
1回の振りで3行が出力され、ダミーに30のダメージを与えました。そのルート、脚、頭は3つのパーツで、すべてボックス内にあり、それぞれが同じ Humanoid にダメージを与えました。
テーブルを使えば解決できます。この振りでヒットした Humanoid をすべて記録し、すでに記録したものはスキップします。テーブルのキーにはインスタンスを使えます。
local hit = {}
if humanoid and not hit[humanoid] then
hit[humanoid] = true
-- 一度だけダメージを与える
endテーブルは swing の中で作られるため、振るたびに空のテーブルから始まり、ボックス内の2体の敵にはそれぞれ1回ずつヒットします。
チャレンジ
簡単1回の振りで各敵に1回ずつ攻撃します。
swing内で、ループの前にlocal hit = {}を追加します。if humanoid and not hit[humanoid]の場合にのみHumanoidにダメージを与え、そのときhit[humanoid] = trueを設定します。
「プレイ」を押してダミーを攻撃します。「Output」に「Dummy is at 90」と1回表示されます。その後、「確認」を押します。
自分で試してみよう
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)
for _, part in workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params) do
local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
-- レッスン4:Once per Swingはここに入れる
if humanoid then
humanoid:TakeDamage(Settings.SwordDamage)
print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
end
end
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)
Robloxゲーム:バトルグラウンドのすべてのレッスン
自分で練習してみよう: Robloxプレイグラウンド