Menu
CoddyTech

Server Cooldowns

Lesson 8 of 25 in Coddy's Roblox Game: Battlegrounds course.

An exploiter controls their own device, so they can fire Remotes.Ability a hundred times a second, with any name. A cooldown in the LocalScript would stop only honest players: the server has to enforce it.

The server keeps, for each player and ability, when it is ready again, as an attribute of the player (the next lesson shows it on screen). workspace:GetServerTimeNow() is a clock the server and every client agree on:

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

And it checks what it was sent: a string naming a real ability, from a player who is alive.

challenge icon

Challenge

Medium

Make the server refuse what an exploiter could send.

  1. In the Ability handler, return if typeof(name) ~= "string", the ability does not exist, or the player has no living character.
  2. Return if workspace:GetServerTimeNow() is before player:GetAttribute(name .. "ReadyAt") (or 0).
  3. Else set that attribute to now + Settings.Cooldowns[name], then run the ability.

Press Play and press Q twice: the second dash waits 3 seconds. Then Check.

Try it yourself

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local remotes = game.ReplicatedStorage.Remotes
local lastSwing = {} -- player -> when they last swung

-- Roblox's own sword animations
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"

print("Swords deal " .. Settings.SwordDamage .. " damage")

-- A swing: the animation, then everyone in the box in front of you is hit
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()
	-- the box in front of you, never your own body
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local box = root.CFrame * CFrame.new(0, 0, -3)
	local hit = {} -- each humanoid once per swing
	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

-- The abilities, by name: what each one does
local abilities = {}

-- a player's own device moves their character, so the dash happens there
function abilities.Dash(player)
	remotes.Ability:FireClient(player, "Dash")
end

-- The client asks for an ability by name; the server does it
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
	-- Lesson 8: Server Cooldowns goes here
end)

-- Every player gets a sword every life
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)

-- The training dummy: its health bar follows its health, and a beaten
-- dummy comes back after a moment, a fresh copy in the same place
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)

All lessons in Roblox Game: Battlegrounds

Practice on your own: Roblox playground