Menu
CoddyTech

휘두를 때마다 한 번

Coddy의 Roblox 게임: 배틀그라운드 코스 레슨. 25개 중 4번째.

한 번 휘둘렀는데 세 줄이 출력되고 더미의 체력이 30 줄었습니다. 더미의 뿌리, 다리, 머리는 상자 안에 있는 세 부분이며, 각 부분이 모두 같은 Humanoid에게 피해를 입혔습니다.

테이블을 사용하면 해결할 수 있습니다. 이 휘두르기로 맞힌 Humanoid를 모두 기억하고, 이미 본 Humanoid는 건너뜁니다. 테이블의 키로 인스턴스를 사용할 수 있습니다.

local hit = {}
if humanoid and not hit[humanoid] then
	hit[humanoid] = true
	-- 한 번만 피해를 입혀라
end

테이블은 swing 안에서 만들어지므로 휘두를 때마다 빈 테이블로 시작하고, 상자 안에 있는 적 두 명은 각각 한 번씩만 맞습니다.

challenge icon

챌린지

쉬움

한 번 휘두를 때마다 각 적을 한 번씩만 공격하세요.

  1. swing에서 반복문 앞에 local hit = {}를 추가하세요.
  2. if humanoid and not hit[humanoid]일 때만 Humanoid에게 피해를 주고, 그렇게 할 때 hit[humanoid] = true로 설정하세요.

‘플레이’를 누르고 훈련용 더미를 공격하세요. Output에 Dummy is at 90이 한 번 표시됩니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

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: 휘두를 때마다 한 번만 처리하는 코드를 여기에 작성
		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 플레이그라운드