The wrench: tools, raycasts, tags

Make a melee weapon for Roblox that cannot be cheated. Build Beacon Yard's wrench Tool, send a swing request, validate it on the server and hit the drone with a raycast filtered by a CollectionService tag.

Project lesson, about 70 minutes, 10 steps

By the end you can

  • Build a Tool with a Handle and give it to every player through StarterPack
  • Validate a swing request on the server and decide the hit with a filtered raycast
  • Use CollectionService tags so the ray can only see the drone

Before this: The drone: movement, targeting, damage, Security: never trust the client

Step 1 of 10

Three hits and the drone is scrap

Equip the wrench with 1, face the drone and click. Its health bar drops by a third, it flashes white and freezes. Two more hits and it breaks apart. Swing at the floor and nothing happens except the sound.

Three accepted hits. Each one deals 34 and stuns the drone.

Transcript
  1. 0:03 Each accepted hit deals 34.
  2. 0:04 Health goes from 100 to 66, then to 32.
  3. 0:05 The third hit destroys the drone. The swings are spaced past the 0.4 second gap.
Work it out

DRONE_MAX_HEALTH is 100 and WRENCH_DAMAGE is 34. How many hits destroy a fresh drone?

hits

Link to this step · This confused me

Step 2 of 10

A Tool needs a Handle

A Tool is an object a player carries and equips. Roblox shows it in the backpack hotbar, and when equipped it moves from the player's Backpack into their character. A tool that is held in the hand needs a part named exactly Handle as a direct child1.

	local tool = Instance.new("Tool")
	tool.Name = "Wrench"
	tool.CanBeDropped = false
	tool.RequiresHandle = true
 
	local template = AssetModels.template("Wrench")
	local uploaded = if template ~= nil then template:Clone() else nil
	local meshHandle = if uploaded ~= nil then uploaded:FindFirstChild("Handle", true) else nil
	if uploaded ~= nil and meshHandle ~= nil and meshHandle:IsA("BasePart") then
		meshHandle.CanCollide = false
		meshHandle.CanTouch = false
		meshHandle.CanQuery = false
		meshHandle.Massless = true
		meshHandle.Parent = tool
		uploaded:Destroy()
	else
		if uploaded ~= nil then
			uploaded:Destroy()
		end
		local handle = Instance.new("Part")
		handle.Name = "Handle"
		handle.Size = Vector3.new(1, 1, 3)
		handle.Color = Color3.fromRGB(192, 144, 64)
		handle.Material = Enum.Material.Metal
		handle.CanCollide = false
		handle.Massless = true
		handle.Parent = tool
	end
 
	tool.Parent = StarterPack

StarterPack copies its tools into each player's Backpack when they spawn. createWrench also gives the tool to players who are already in the server, which is why it runs before the map is built: a character that spawns before the wrench exists would arrive empty-handed.

Roblox Studio
Beacon Yard during Play with the wrench equipped in the character's hand and the Wrench slot in the hotbar.

Select a numbered pin, or a row below, to read what it marks.

The wrench in hand. Select the pins for the Handle and the hotbar slot.

Choose one

The handle part is named Grip instead of Handle. RequiresHandle is true. What happens when a player equips the wrench?

The handle part is named Grip instead of Handle. RequiresHandle is true. What happens when a player equips the wrench?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 3 of 10

A swing is a request, not a hit

The client decides where you aimed. It fires the Swing remote with two vectors, an origin near your head and a unit direction, and nothing else. It never sends damage and never decides whether it hit.

	local function fireSwing()
		local character = player.Character
		local camera = workspace.CurrentCamera
		if character == nil or camera == nil then
			return
		end
		local origin, direction = aim(character, camera)
		if origin == nil or direction == nil then
			return
		end
		playSwing(character)
		swing:FireServer(origin, direction)

The server's trySwing treats both vectors as untrusted. It checks their types, rejects infinite or zero-length vectors, requires the wrench to be in the character, and rejects an origin more than 8 studs from the head. It also enforces a 0.4-second gap between swings.

	if (origin - head.Position).Magnitude > ORIGIN_MAX_STUDS then
		return "rejected"
	end
 
	local now = workspace:GetServerTimeNow()
	local previousSwing = lastSwing[player]
	if previousSwing ~= nil and now - previousSwing < SWING_GAP then
		return "rejected"
	end
	lastSwing[player] = now
Luau
Ctrl Enter runs the cell. Checked against the expected output
Choose all that apply

Which values does the client send with Swing:FireServer?

Which values does the client send with Swing:FireServer?

Press 1 to 5 to choose, Enter to check.

Link to this step · This confused me

Step 4 of 10

The server fires a laser

Once the request passes, the server decides the hit with a RaycastRaycastA test that traces a straight line through the world and reports the first part it meets.Think of it as a laser pointer.. workspace:Raycast(origin, direction, params) traces a line from origin along direction and returns the first part it meets, or nil2. The direction is the whole line, so its length is the range: the server multiplies the unit direction by WRENCH_RANGE, 14 studs.

Both sidesA raycast is a laser pointer.
In the pictureIn Roblox
Where you hold the pointerThe origin
Where you point it, and how far it reachesThe direction vector and its length
The red dotThe RaycastResult: the part, position and normal of the hit

See the whole analogy cast

A raycast is a laser pointer. Point it from a spot in a direction, and it tells you the first thing the dot lands on.

Think of it as A laser pointer

	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Include
	params.FilterDescendantsInstances = CollectionService:GetTagged(DRONE_TAG)
 
	local result = workspace:Raycast(origin, direction.Unit * Config.WRENCH_RANGE, params)
	if result == nil or not isTaggedDrone(result.Instance) then
		return "miss"
	end
 
	DroneService.damageFromWrench(Config.WRENCH_DAMAGE)
	feedback(player, "hit", nil)
	return "hit"
Find the broken line

After a refactor the wrench never hits a drone more than a stud away. Which line is wrong?

After a refactor the wrench never hits a drone more than a stud away. Which line is wrong?

Press 1 to 5 to choose, Enter to check.

Link to this step · This confused me

Step 5 of 10

Tags: a coloured sticker on the drone

The ray's filter is an Include list built from CollectionService:GetTagged("Drone"). DroneService adds the Drone Tag (CollectionService)Tag (CollectionService)A string label on an instance that scripts can search for.Think of it as a coloured sticker. to the drone's root when it spawns. Tags replicate from the server to clients, and any script can ask for every instance with a tag3.

A tag is a coloured sticker. Stick a red dot on every drone and you can ask the room for "everything with a red dot" without knowing where each one is.

Think of it as Coloured stickers

Both sidesRemember: CollectionService tags is coloured stickers.

With FilterType = Include, the ray considers only the drone's parts. The floor, walls and players are invisible to it. That is why swinging at the ground is always a miss.

A swing at the ground: accepted, so the sound plays, but the ray finds nothing tagged Drone.

Transcript
  1. 0:00 The server accepted the swing, so the swing sound can play.
  2. 0:01 The include list is only the Drone tag. The ground is not a hit.
  3. 0:04 The bar stays where it was.
Predict the output

The drone is 10 studs away, behind the 2-stud wall. You swing straight at it. What does the server decide?

params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = CollectionService:GetTagged("Drone")
local result = workspace:Raycast(origin, direction.Unit * 14, params)
The drone is 10 studs away, behind the 2-stud wall. You swing straight at it. What does the server decide?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 6 of 10

Hit confirm: telling a hit from a miss

Because the server decides the hit, the player needs a clear hit confirm so a miss and a hit never feel alike. Look at the two feedback(...) calls inside CombatService.trySwing:

	feedback(player, "swing", nil)
 
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Include
	params.FilterDescendantsInstances = CollectionService:GetTagged(DRONE_TAG)
 
	local result = workspace:Raycast(origin, direction.Unit * Config.WRENCH_RANGE, params)
	if result == nil or not isTaggedDrone(result.Instance) then
		return "miss"
	end
 
	DroneService.damageFromWrench(Config.WRENCH_DAMAGE)
	feedback(player, "hit", nil)
	return "hit"

Every accepted swing fires Feedback with "swing", even when the ray misses. Only a confirmed hit on a tagged drone fires "hit". At m06-drone-wrench, SoundClient plays Config.SOUNDS.swing on "swing" and Config.SOUNDS.hit on "hit", while passing nil as the second argument leaves the HUD silent. Passing a short confirm string (such as "-34") triggers the HUD toast on every hit too, so even a muted player sees the confirmation. From m07-feel onward VfxClient also listens to "hit" for a burst of sparks, and Module 14's Game feel: juice, hit stop and feedback adds hit stop, reduced-motion-aware camera shake, damage numbers and layered audio on top of this same server confirm.

Luau with roblox-lite (not Roblox)
Ctrl Enter runs the cell. Checked against the expected output
Choose one

Why does CombatService fire Feedback with "hit" only after the server raycast succeeds, instead of letting CombatClient play the hit confirm immediately on click?

Why does CombatService fire Feedback with "hit" only after the server raycast succeeds, instead of letting CombatClient play the hit confirm immediately on click?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 7 of 10

Where the laser picture breaks

A laser pointer lights up the first thing in its path, whatever it is. A raycast does too, but only among the things its filter lets it see. Answer first, then open the breakdown.

Both sidesRemember: A raycast is a laser pointer.

Where does it break?“A laser pointer” is a good picture of a raycast. Where does it stop being true?

It breaks here. It reports only the first hit, and filters make whole groups of parts invisible to it, so it can pass through walls or stop on the wrong thing.

The true model. Raycast tests one segment against the parts its RaycastParams allow and returns the nearest intersection or nil. Include lists see only listed parts; Exclude lists see everything else. To respect cover, the ray must be able to see the cover.

Choose one

You change the filter to Exclude the player's own character, so the ray sees everything else. The drone is behind a crate. What does Raycast return?

You change the filter to Exclude the player's own character, so the ray sees everything else. The drone is behind a crate. What does Raycast return?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 8 of 10

Build it: add the wrench

Other ways pros do this

OptionWhat it isWhen pros pick itTrade-offs
RaycastOne thin line from the player along the aim.Precise melee and hitscan weapons. What Beacon Yard uses.A thin line can slip past a small target; the aim has to be good.
ShapecastBlockcast or Spherecast sweeps a shape along the line.Forgiving melee swings and thick projectiles.Slightly more costly, and the shape size needs tuning.
Hitbox queryGetPartBoundsInBox or GetPartsInPart around the weapon at the moment of the swing.Wide swings that should hit anything in an arc.Easy to make too generous; still must run on the server.
Touched on the weaponThe blade's Touched event during an animation.Quick prototypes.Fires on the client's simulated character, misses fast swings and is easy to exploit. Studios avoid it for damage.
  • src/server/CombatService.luau
  • src/client/CombatClient.luau
  • src/server/Remotes.luau
  • src/server/init.server.luau
  1. In the game repo, run git diff m05-the-round m06-drone-wrench -- src/server/Remotes.luau and git diff --stat m05-the-round m06-drone-wrench -- src/server/CombatService.luau src/client/CombatClient.luau.

    You should see CombatService.luau and CombatClient.luau are new, and Remotes.luau's REMOTE_NAMES gains ShopOpen, ShopBuy and Swing.

  2. Check out m06-drone-wrench and open src/server/init.server.luau.

    You should see init.server.luau calls CombatService.start() before MapBuilder.build(), with a comment explaining why.

  3. Run rojo serve, connect Studio and press Play.

    You should see StarterPack holds a Tool named Wrench during Play, and the hotbar shows Wrench in slot 1.

  4. Start a drill, press 1 to equip the wrench and click while facing the drone.

    You should see Three hits destroy the drone. A swing at the floor plays the sound but does not change the health bar.

Link to this step · This confused me

Step 9 of 10

Challenges

Level 1, Apprentice

Challenge

Shorten the wrench to 4 studs and find the new sweet spot, then restore it.

Start from
The game at milestone m06 (download)

Done means

  • With WRENCH_RANGE at 4 you have to stand close to land a hit
  • WRENCH_RANGE is 14 again afterwards

Meet the criteria above, then record your own result. No test grades this check.

Choose one

An exploiter fires Swing with an origin right next to the drone, 30 studs from their head. What does trySwing return?

An exploiter fires Swing with an origin right next to the drone, 30 studs from their head. What does trySwing return?

Press 1 to 4 to choose, Enter to check.

Level 2, Builder

Challenge

Make walls block the wrench.

Start from
The game at milestone m06 (download)

Done means

  • A swing at the drone through a wall is a miss
  • A clear swing still hits, and wrench_kill still passes

Meet the criteria above, then record your own result. No test grades this check.

Level 3, Professional

Challenge

Add a charged swing whose strength the server times.

Start from
The game at milestone m06 (download)
Grade it
To grade work from an earlier milestone, apply your challenge changes to a checkout based on m14-ship, which includes the Course Runner. In Studio, press Play, then run game.ServerScriptService.RunGameTests:Invoke("wrench", "wrench-charge") in the server command bar. Copy the pass code from Output.

Done means

  • CombatService exposes beginCharge(player) to record when that player started holding a charge on the server
  • CombatService.trySwing(player, origin, direction, charge?) accepts an optional 4th charge argument in seconds, capped at 1.2s
  • trySwing returns "rejected" and deals 0 damage if charge > 1.2 or if the player has not held the charge long enough on the server
  • A swing with charge = nil deals Config.WRENCH_DAMAGE (34), even right after a rejected charge, so the plain tap and the existing wrench_kill test still work
  • A full 1.2s earned charge deals Config.WRENCH_DAMAGE + 16, and a charge below a small minimum (the reference solution uses 0.45s) is rejected
  • The swing gap still applies between swings: the test waits 0.5s between the tap and the full-charge swing
  • challenges/wrench.luau prints TEST PASS for charge_reject_high, charge_reject_unearned, charge_tap and charge_full

The Course Runner prints this code when the test passes.

Link to this step · This confused me

Step 10 of 10

Recap

Retiring the analogy

You no longer need “a laser pointer”. Raycast tests one segment against the parts its RaycastParams allow and returns the nearest intersection or nil. Include lists see only listed parts; Exclude lists see everything else. To respect cover, the ray must be able to see the cover.

Recap

  1. The wrench is a Tool with a part named Handle, given to every player through StarterPack before the first spawn.
  2. The client sends only an origin and a direction; the server checks the tool, the 8-stud origin, the 0.4-second gap, and casts a 14-stud ray.
  3. The ray's Include filter is the set of parts tagged Drone, so it sees only the drone and returns the first hit among them.

A raycast is a laser pointer, until it reports only the first hit, and filters make whole groups of parts invisible to it, so it can pass through walls or stop on the wrong thing.

CollectionService tags is coloured stickers, until a sticker does nothing until code looks for it, and a tag on the drone's root does not tag its children.

Next First publish: private cloud place, API access and Ids.local

Link to this step · This confused me