Start building

How to make a sword combat system in Roblox: hitboxes, cooldowns and damage

A Tool, one RemoteEvent and a server-side hitbox: build a sword that combos, knocks enemies back and keeps every damage check where cheaters can't reach.

A King Slime boss fight in Roblox Studio: the boss health bar, a damage number and the BossController script Revix wrote.
On this page
  1. How a secure sword works
  2. Set up the sword Tool
  3. Check every swing on the server
  4. Build the hitbox and deal damage
  5. Attack animations and a 3-hit combo
  6. Knockback and network ownership
  7. Hit feedback: sound and a red flash
  8. Stop exploiters
  9. Questions

A Roblox sword script has two halves. A LocalScript inside the Tool listens to Tool.Activated, plays the swing animation and fires a RemoteEvent. A server Script checks that swing, builds a hitbox in front of the player and deals the damage. The server half is what turns a sword into a fair combat system.

You should be comfortable with functions and events before you start; Roblox scripting for beginners covers both. If this is your first game, start with how to make a Roblox game.

How a secure sword works

Every swing follows the same path:

  1. The player clicks, and the Tool fires Activated on their client.
  2. The LocalScript plays the animation and swing sound right away, so it feels instant, then fires the SwordSwing RemoteEvent with no arguments.
  3. The server checks the swing: the player is alive, holds the sword and is off cooldown.
  4. After a short delay, the server looks for characters inside a box in front of the player and damages each one once.
  5. The server shows the hit with a flash and a sound, and knocks the target back on the combo finisher.

The client never says who it hit or how much damage to deal. Anything a client sends can be faked, so the only thing it reports is "I swung".

Set up the sword Tool

  1. In Explorer, add a Tool to StarterPack and name it Sword. In Properties, turn off CanBeDropped so players can't drop it.
  2. Inside the Tool, add a Part (or your sword mesh) named Handle with CanCollide off. The character holds the Handle in its right hand; adjust the Tool's Grip properties if it sits wrong.
  3. Inside Handle, add a Sound named Swing and set its SoundId to rbxasset://sounds/swordslash.wav, a slash sound that ships with Roblox.
  4. In ReplicatedStorage, add two RemoteEvents named SwordSwing and Knockback.
  5. Add a LocalScript inside the Tool:
LocalScriptStarterPack
-- LocalScript inside StarterPack > Sword: swing on click and play the combo animation
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local tool = script.Parent
local swingEvent = ReplicatedStorage:WaitForChild("SwordSwing")

local COOLDOWN = 0.45 -- seconds between swings
local COMBO_RESET = 1.2 -- a longer pause starts the combo over
local ANIMATION_IDS = { "rbxassetid://0", "rbxassetid://0", "rbxassetid://0" } -- your 3 swings

local tracks, loadedFor = {}, nil
local lastSwing, step = 0, 0

local function loadTracks()
	local humanoid = tool.Parent:FindFirstChildOfClass("Humanoid")
	local animator = humanoid and humanoid:FindFirstChildOfClass("Animator")
	if animator and animator ~= loadedFor then
		loadedFor = animator
		for i, id in ANIMATION_IDS do
			local animation = Instance.new("Animation")
			animation.AnimationId = id
			tracks[i] = animator:LoadAnimation(animation)
			tracks[i].Priority = Enum.AnimationPriority.Action
		end
	end
end

tool.Equipped:Connect(loadTracks)

tool.Activated:Connect(function()
	local now = os.clock()
	if now - lastSwing < COOLDOWN then
		return
	end
	step = if now - lastSwing > COMBO_RESET then 1 else step % 3 + 1
	lastSwing = now
	if tracks[step] then
		tracks[step]:Play()
	end
	tool.Handle.Swing:Play()
	swingEvent:FireServer()
end)

Activated only fires while the sword is equipped. The cooldown and combo counter here just stop fast clickers from spamming the remote and restarting the animation; the server keeps its own copy, which decides damage. Tracks load once per character, since an Animator holds a limited number of them.

Check every swing on the server

Add a Script to ServerScriptService named SwordServer. Part 1 decides whether a swing is allowed and which combo step it is:

ScriptServerScriptService
-- Script in ServerScriptService named SwordServer, part 1: is this swing allowed?
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local swingEvent = ReplicatedStorage:WaitForChild("SwordSwing")

local COOLDOWN = 0.4 -- a little under the client's 0.45, to absorb network jitter
local COMBO_RESET = 1.2
local lastSwing = {} -- player -> time of their last swing
local comboStep = {} -- player -> 1, 2 or 3

local function canSwing(player)
	local character = player.Character
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")
	local tool = character and character:FindFirstChildOfClass("Tool")
	if not humanoid or humanoid.Health <= 0 or not tool or tool.Name ~= "Sword" then
		return false
	end
	local now = os.clock()
	local since = now - (lastSwing[player] or 0)
	if since < COOLDOWN then
		return false
	end
	comboStep[player] = if since > COMBO_RESET then 1 else (comboStep[player] or 0) % 3 + 1
	lastSwing[player] = now
	return true, character
end

Players.PlayerRemoving:Connect(function(player)
	lastSwing[player] = nil
	comboStep[player] = nil
end)

Each check closes a hole. A dead player can't swing. A player who isn't holding the sword (an equipped Tool sits inside the character) can't fire the remote to hit people. The per-player lastSwing time stops auto-clickers and exploit scripts that fire the event 100 times a second, and the slightly shorter server cooldown means a real swing that arrives a few milliseconds early still counts.

Build the hitbox and deal damage

Part 2 goes under part 1 in the same Script. It builds the hitbox and damages everything inside it once, plus the knockback and feedback explained in the next sections.

Script
-- Same Script, part 2: hitbox, damage, knockback and hit feedback
local COMBO_DAMAGE = { 10, 10, 20 }
local HITBOX_SIZE = Vector3.new(5, 5, 6) -- width, height and reach in studs
local HIT_DELAY = 0.15 -- let the blade come around before checking
local KNOCKBACK = 40 -- studs per second on the third hit
local knockbackEvent = ReplicatedStorage:WaitForChild("Knockback")

local function flash(model, root)
	local highlight = Instance.new("Highlight")
	highlight.FillColor = Color3.fromRGB(255, 60, 60)
	highlight.OutlineTransparency = 1
	highlight.Parent = model
	local sound = Instance.new("Sound")
	sound.SoundId = "rbxasset://sounds/hit.wav"
	sound.Parent = root
	sound:Play()
	task.delay(0.15, function() highlight:Destroy() end)
	task.delay(2, function() sound:Destroy() end)
end

local function swing(character, step)
	local root = character:FindFirstChild("HumanoidRootPart")
	if not root then
		return
	end
	local params = OverlapParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = { character }
	local box = root.CFrame * CFrame.new(0, 0, -HITBOX_SIZE.Z / 2 - 1)

	local alreadyHit = {}
	for _, part in workspace:GetPartBoundsInBox(box, HITBOX_SIZE, params) do
		local model = part:FindFirstAncestorOfClass("Model")
		local humanoid = model and model:FindFirstChildOfClass("Humanoid")
		local targetRoot = model and model:FindFirstChild("HumanoidRootPart")
		if humanoid and targetRoot and humanoid.Health > 0 and not alreadyHit[humanoid] then
			alreadyHit[humanoid] = true
			humanoid:TakeDamage(COMBO_DAMAGE[step])
			flash(model, targetRoot)
			if step == 3 then
				local push = (root.CFrame.LookVector + Vector3.new(0, 0.4, 0)) * KNOCKBACK * targetRoot.AssemblyMass
				local victim = Players:GetPlayerFromCharacter(model)
				if victim then
					knockbackEvent:FireClient(victim, push) -- their device owns their physics
				else
					targetRoot:ApplyImpulse(push)
				end
			end
		end
	end
end

swingEvent.OnServerEvent:Connect(function(player)
	local ok, character = canSwing(player)
	if ok then
		task.delay(HIT_DELAY, swing, character, comboStep[player])
	end
end)

root.CFrame * CFrame.new(0, 0, -HITBOX_SIZE.Z / 2 - 1) puts the box in front of the player, because negative Z is forward in a CFrame. OverlapParams excludes the attacker, so you never hit yourself. A box usually touches several parts of one character (arms, torso, accessories), so alreadyHit makes sure each Humanoid takes damage once per swing. TakeDamage also respects ForceFields, so freshly spawned players stay protected. HIT_DELAY should match the moment the blade crosses the body in your animation; if hits feel early or late, that is the one number to change.

There are three common ways to detect a hit:

MethodWhen to use it
workspace:GetPartBoundsInBoxA box in front of the player: cheap and predictable, used here
workspace:GetPartsInPartAn exact shape, such as an invisible part welded along the blade
Touched on the bladeAvoid it for damage: it misses fast swings and is easy for exploiters to trigger

Attack animations and a 3-hit combo

Roblox only plays animations you created or own: the animation must belong to the same account or group that owns the experience, or it won't play. Make three short swings in Studio's Animation Editor, publish each one to Roblox, and paste their ids into ANIMATION_IDS in the LocalScript. The script sets each track to the Action priority, so it plays over walking and idle.

The combo is two quick slashes and a heavy finisher: COMBO_DAMAGE is 10, 10 and 20, and only step 3 knocks back. Both sides count 1, 2, 3 with the same rules. A swing within 1.2 seconds of the last one moves to the next step; a longer pause starts over at 1.

The client's count only picks which animation plays. The server's count picks the damage, so a cheater who edits their client changes nothing but their own animation.

Knockback and network ownership

On the third hit, part 2 builds an impulse: the attacker's forward direction plus a little lift, times KNOCKBACK, times the target's AssemblyMass. Multiplying by mass means light and heavy characters fly the same distance.

Who applies the impulse matters. Roblox lets each player's device simulate their own character; this is called network ownership. An impulse the server applies to another player's HumanoidRootPart can be overwritten by that player's device a moment later. So for players, the server sends the impulse to the victim's own client, which owns the physics:

LocalScriptStarterPlayerScripts
-- LocalScript in StarterPlayerScripts: apply knockback the server sends us
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage:WaitForChild("Knockback").OnClientEvent:Connect(function(push)
	local character = Players.LocalPlayer.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if root and typeof(push) == "Vector3" then
		root:ApplyImpulse(push)
	end
end)

NPCs are usually simulated by the server, so part 2 calls ApplyImpulse on them directly. One catch: an NPC standing near a player can be handed to that player's device too. Call SetNetworkOwner(nil) on each NPC's HumanoidRootPart when you spawn it, and server knockback always lands.

Hit feedback: sound and a red flash

Feedback is what makes combat feel good, and it is cheap. On every hit, flash in part 2 adds a red Highlight to the target for 0.15 seconds and plays rbxasset://sounds/hit.wav from the target's position. Both are created on the server, so every player sees and hears them. Roblox only draws a limited number of Highlights at once, so keep the flash short and always destroy it.

More ideas that cost almost nothing:

  • A small camera shake on the attacker's screen when a hit lands.
  • Hit-stop: slow the swing animation for a split second on contact with AnimationTrack:AdjustSpeed.
  • Damage numbers in a BillboardGui that float up and fade with TweenService.

In big battles, move effects to clients: the server fires one RemoteEvent with FireAllClients, and each client draws the flash and plays the sound itself. Revix can wire this kind of polish into an existing sword, in the style your game already has.

Stop exploiters

The scripts above already follow the rules that matter. Keep following them as the combat system grows:

  • Trust nothing the client sends. The remote carries no target, damage or position. If you add a parameter later, like an aim direction, type-check it and clamp it on the server.
  • Validate every call. Alive, equipped and off cooldown, every single time, not just the first.
  • Keep the hitbox on the server. If you ever move it to the client for feel, the server must re-check distance and cooldown for every hit the client reports.
  • Skip teammates. In team games, get the victim with Players:GetPlayerFromCharacter(model) and compare Team before dealing damage.
  • Know what the sword can't stop. Players control their own character's movement, so a cheater can still teleport next to someone. Catch that with movement checks elsewhere.
  • Log before you kick. Lag makes honest players look strange now and then. Count rejected swings per player and act on patterns, not on a single odd one.

The same rule, damage only on the server, runs every kind of combat, including a tower defense game where towers land the hits instead of swords.

Questions

How do you make a sword do damage in Roblox?

Fire a RemoteEvent from the Tool's LocalScript when Tool.Activated runs, then do everything else on the server: check the player is alive, holding the sword and off cooldown, find characters in a box in front of them with GetPartBoundsInBox, and call TakeDamage on each Humanoid once.

Should a Roblox hitbox run on the client or the server?

Start on the server, because the server can't be lied to about who was hit. Client hitboxes can feel snappier on laggy connections, but then the server has to re-check every reported hit for distance and cooldown, which is more code to get right.

Why won't my sword animation play?

Usually it's ownership: Roblox only plays animations owned by the account or group that owns the experience. Also check that the track is loaded through the character's Animator and that its priority is Action, so walking and idle animations don't override it.

How do I stop exploiters from spamming my sword?

Keep a cooldown per player on the server and ignore swings that arrive too fast, check that the sword is equipped and the player is alive, and never accept a target or damage value from the client. The client should only ever say that it swung.

Your game is
one sentence away.

Start building free

Free to start. No card needed.