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

1Three 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.

Figure 1.1.

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.

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

Answer

3 hits. 100, 66, 32, then -2. The third hit takes it below zero. The suite's wrench_kill test swings exactly three times.

Open this step in lesson mode · Link to this step · This confused me

2A 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
Figure 2.1

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.

Figure 2.2.

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

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

  1. The character holds nothing, because the tool has no Handle to weld to the hand
  2. Roblox finds the first part and uses it
  3. The tool refuses to go in the backpack
  4. The server errors
Answer

The character holds nothing, because the tool has no Handle to weld to the hand. The engine looks for a child named Handle. Without one, a RequiresHandle tool has nothing to attach to the hand.

Open this step in lesson mode · Link to this step · This confused me

3A 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)
Figure 3.1

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
Figure 3.2
Luau
local SWING_GAP = 0.4
local lastSwing = nil
local accepted = 0
for _, now in { 0, 0.2, 0.5, 0.6, 1.0 } do
	if lastSwing == nil or now - lastSwing >= SWING_GAP then
		lastSwing = now
		accepted += 1
	end
end
print("accepted", accepted)
Figure 3.3. Expected output: accepted 3

Which values does the client send with Swing:FireServer?

  1. An origin (Vector3)
  2. A unit direction (Vector3)
  3. The damage to deal
  4. Whether the ray hit the drone
  5. The player who swung
Answer

An origin (Vector3); A unit direction (Vector3). Only origin and direction. The engine adds the Player as the first argument of OnServerEvent, and the server works out the hit and the damage.

Open this step in lesson mode · Link to this step · This confused me

4The 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"
Figure 4.1

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

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = CollectionService:GetTagged(DRONE_TAG)
local result = workspace:Raycast(origin, direction.Unit, params)
if result == nil or not isTaggedDrone(result.Instance) then return "miss" end
Answer

Line 4: local result = workspace:Raycast(origin, direction.Unit, params). Fixed: local result = workspace:Raycast(origin, direction.Unit * Config.WRENCH_RANGE, params). Raycast's second argument is the full segment. A unit vector is 1 stud long, so the ray stops after 1 stud. Multiply by WRENCH_RANGE to reach 14 studs.

Open this step in lesson mode · Link to this step · This confused me

5Tags: 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.

Figure 5.1.

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.

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)
  1. A hit: the wall is not in the Include list, so the ray passes through it
  2. A miss: the wall blocks the ray
  3. Rejected: the drone is too far
  4. An error: the ray starts inside a part
Answer

A hit: the wall is not in the Include list, so the ray passes through it. Include filters make every other part invisible to the ray, walls included. Beacon Yard accepts that because the yard is open. A game with cover would add the walls to the list and check that the first hit is the drone.

Open this step in lesson mode · Link to this step · This confused me

6Hit 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"
Figure 6.1

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)
local feedback = Signal.new()
feedback:Connect(function(kind, text)
	if kind == "swing" then
		print("audio: swing swoosh")
	elseif kind == "hit" then
		print("audio: hit clang | hud:", text or "(silent)")
	end
end)
 
local function trySwing(hitDrone)
	feedback:Fire("swing", nil)
	if not hitDrone then
		return "miss"
	end
	feedback:Fire("hit", "-34")
	return "hit"
end
 
print("swing 1 ->", trySwing(false))
print("swing 2 ->", trySwing(true))
Figure 6.2. Expected output: audio: swing swoosh swing 1 -> miss audio: swing swoosh audio: hit clang | hud: -34 swing 2 -> hit

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

  1. Only the server knows whether the swing passed validation and hit the drone, so a client-side confirm would lie on misses and rejected swings
  2. Clients cannot play sounds unless a RemoteEvent fires first
  3. Feedback only accepts the string "hit" on the server
  4. The raycast cannot run until after SoundClient finishes playing
Answer

Only the server knows whether the swing passed validation and hit the drone, so a client-side confirm would lie on misses and rejected swings. Swing cues can feel instant on click, while hit confirmation must wait for the server's raycast verdict so a miss or a rejected swing never reports a fake hit.

Open this step in lesson mode · Link to this step · This confused me

7Where 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.

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?

  1. The crate, the first part on the line; the drone is never reported
  2. The drone, because it is tagged
  3. Both the crate and the drone
  4. Nil, because two parts are in the way
Answer

The crate, the first part on the line; the drone is never reported. Raycast returns only the first hit. With an Exclude filter the crate is first, and isTaggedDrone(crate) is false, so the swing is a miss. Cover works; hitting through walls does not.

Open this step in lesson mode · Link to this step · This confused me

8Build 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.

Open this step in lesson mode · Link to this step · This confused me

9Challenges

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

Hints

  1. WRENCH_RANGE is in Config.luau.
  2. The client uses it too, to aim; both sides read the same Config.

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

  1. "rejected"
  2. "hit"
  3. "miss"
  4. Nothing; it errors
Answer

"rejected". The origin must be within 8 studs of the head. A 30-stud origin is rejected before any ray is cast, so the exploit cannot reach across the yard.

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

Hints

  1. Switch the filter to Exclude and put the swinging character in the list.
  2. isTaggedDrone already checks that the first hit belongs to the drone.

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

Hints

  1. Store the charge start time per player inside CombatService when beginCharge(player) is called, and measure elapsed time on the server.
  2. Allow a small timing grace window on the server check (the test waits 1.15s before requesting 1.2s, and 0.12s before requesting 1s).
  3. Scale bonus damage linearly from the base at the minimum charge up to +16 at 1.2s, and keep charge = nil equal to WRENCH_DAMAGE so existing tests pass.
  4. Read challenges/solutions/wrench.md only after you try.

Open this step in lesson mode · Link to this step · This confused me

10Recap

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

Open this step in lesson mode · Link to this step · This confused me