Menu
CoddyTech

서버에 요청하기

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

능력은 플레이어의 기기에서 시작됩니다. BattleClient는 HUD의 LocalScript입니다. ContextActionService는 동작을 키에 연결하며, 세 번째 인수로 true를 지정하면 휴대폰 화면에 해당 동작을 위한 버튼도 표시합니다:

ContextActionService:BindAction("Wave", use, true, Enum.KeyCode.T)

함수는 동작의 이름과 입력 상태를 받으며, 키를 누르면 Begin 상태가 됩니다. 규칙은 서버가 관리하므로 클라이언트는 RemoteEvent Remotes.Ability를 통해 요청만 보냅니다. 서버에서는 OnServerEvent가 이를 수신하고, Roblox는 이를 발생시킨 플레이어를 첫 번째 인수로 전달합니다:

remotes.Ability.OnServerEvent:Connect(function(player, name)
	print(name .. " from " .. player.Name)
end)
challenge icon

챌린지

중급

Q를 눌러 대시를 요청하도록 하세요.

  1. BattleClient에서 use(name, state)를 작성하세요. Enum.UserInputState.Begin일 때 remotes.Ability:FireServer(name)을 호출하세요.
  2. 다음과 같이 바인딩하세요: ContextActionService:BindAction("Dash", use, true, Enum.KeyCode.Q).
  3. BattleServer에서 remotes.Ability.OnServerEvent를 print(player.Name .. " asked for " .. name)에 연결하세요.

‘플레이’를 누르고 Q를 누르세요. 출력에 Player1 asked for Dash가 표시됩니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

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는 한 번씩만 공격당함
	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)
			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)

-- 훈련용 더미: 체력 바는 체력에 따라 변하고, 쓰러진
-- 더미는 잠시 후 같은 자리에 새 복제본으로 돌아옴
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 플레이그라운드