Menu
CoddyTech

서버 쿨다운

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

익스플로잇 사용자는 자신의 기기를 제어하므로, 어떤 이름이든 사용해 Remotes.Ability를 초당 백 번 호출할 수 있습니다. LocalScript의 쿨다운은 정직한 플레이어만 막을 수 있습니다. 서버에서 이를 강제해야 합니다.

서버는 플레이어와 능력별로 다시 사용할 수 있게 되는 시각을 플레이어의 속성으로 저장합니다(다음 레슨에서는 이 값이 화면에 표시됩니다). workspace:GetServerTimeNow()는 서버와 모든 클라이언트가 같은 값을 기준으로 삼는 시계입니다.

local now = workspace:GetServerTimeNow()
if now < (player:GetAttribute(name .. "ReadyAt") or 0) then
	return
end
player:SetAttribute(name .. "ReadyAt", now + Settings.Cooldowns[name])

또한 서버는 전달된 내용도 확인합니다. 실제 능력의 이름인 문자열인지, 그리고 보낸 플레이어가 살아 있는지 확인합니다.

challenge icon

챌린지

중급

익스플로잇 사용자가 보낼 수 있는 요청을 서버가 거부하도록 만드세요.

  1. Ability 처리기에서 typeof(name) ~= "string"이거나, 해당 능력이 존재하지 않거나, 플레이어에게 살아 있는 캐릭터가 없으면 반환하세요.
  2. workspace:GetServerTimeNow()가 player:GetAttribute(name .. "ReadyAt")(또는 0)보다 이르면 반환하세요.
  3. 그렇지 않으면 해당 속성을 현재 시간 + Settings.Cooldowns[name]으로 설정한 다음 능력을 실행하세요.

‘플레이’를 누르고 Q를 두 번 누르세요. 두 번째 대시는 3초 동안 기다립니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

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를 한 번씩만 공격
	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

-- 이름별 능력: 각 능력이 하는 일
local abilities = {}

-- 플레이어의 캐릭터는 플레이어 자신의 기기에서 움직이므로 돌진도 그곳에서 처리
function abilities.Dash(player)
	remotes.Ability:FireClient(player, "Dash")
end

-- 클라이언트가 이름으로 능력을 요청하면 서버가 실행
remotes.Ability.OnServerEvent:Connect(function(player, name)
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if abilities[name] and root then
		abilities[name](player, character, root)
	end
	-- 레슨 8: 서버 쿨다운을 여기에 추가
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 플레이그라운드