Lesson 5 of 12
The drone: movement, targeting, damage
Script an enemy drone for a Roblox game. Move it on the server with Heartbeat and PivotTo, target the nearest player, deal damage with a distance check and a per-player cooldown, and stun it.
By the end you can
- Move an anchored model on the server each frame with Heartbeat and PivotTo
- Pick the nearest living player and stop at a fixed distance
- Explain why the drone hurts by a server distance check instead of Touched, and how its hit cooldown and stun work
Before this: The round loop: a state machine
1Something is chasing you
Start a drill and a hovering drone appears just north of the beacon. It turns toward the nearest player and glides after them at a steady speed. When it catches you, your health bar drops by 15, then again a second later.
The drone during a drill. Everything it does is decided on the server.
Transcript
- 0:02 The server moves the drone on Heartbeat at 12 studs a second.
- 0:04 It stops about 3 studs away, which is inside the 3.4 stud hit radius.
- 0:05 TakeDamage subtracts 15, then the hit waits 1 second.
DroneService arrived at m06-drone-wrench. The round turns it on with setDroneActive(true) when Active starts and off in Cooldown.
Where is the drone's position decided?
Answer
On the server, every frame, in DroneService. The server moves the anchored root with PivotTo on Heartbeat. Clients only receive the new position through replication, so nobody can teleport the drone.
Open this step in lesson mode · Link to this step · This confused me
2One small step every frame
RunService.Heartbeat fires once per frame on the server, after physics, and passes dt, the seconds since the last frame1. moveDrone turns the drone's speed into a distance for this frame: DRONE_SPEED * dt.
local function moveDrone(drone: Drone, dt: number)
local root = drone.root
local pos = Vector3.new(root.Position.X, HOVER_Y, root.Position.Z)
local rotation = root.CFrame.Rotation
local target = nearestLivingRoot(root.Position)
if target then
local flat = Vector3.new(target.Position.X - pos.X, 0, target.Position.Z - pos.Z)
local distance = flat.Magnitude
if distance > STOP_DISTANCE then
local travel = math.min(Config.DRONE_SPEED * dt, distance - STOP_DISTANCE)
local direction = flat.Unit
pos += direction * travel
rotation = CFrame.lookAt(Vector3.zero, direction).Rotation
end
end
drone.model:PivotTo(CFrame.new(pos) * rotation)
The movement ignores height. The drone's Y is pinned to HOVER_Y, 4 studs, and the direction is flattened so it never dives into the floor. PivotTo moves the whole model, mesh and health bar included, to a new CFrame, a position plus a rotation.
DRONE_SPEED is 12 studs per second. At 60 frames per second, how far does the drone move in one frame, in studs?
Answer
0.2 studs (within 0.001). 12 × 1/60 = 0.2 studs per frame. Multiplying by dt keeps the speed the same if the frame rate drops: fewer, bigger steps.
Open this step in lesson mode · Link to this step · This confused me
3Pick the nearest living player
Each frame, nearestLivingRoot loops over every player, skips anyone without a character or with zero health, and keeps the HumanoidRootPart that is closest. The drone then moves toward it, but stops STOP_DISTANCE, 3 studs, short.
local function nearestLivingRoot(fromPosition: Vector3): BasePart?
local best: BasePart? = nil
local bestDistance = math.huge
for _, player in Players:GetPlayers() do
local character = player.Character
if character then
local humanoid = character:FindFirstChildOfClass("Humanoid")
local rootPart = character:FindFirstChild("HumanoidRootPart")
if humanoid and humanoid.Health > 0 and rootPart and rootPart:IsA("BasePart") then
local distance = (rootPart.Position - fromPosition).Magnitude
if distance < bestDistance then
bestDistance = distance
best = rootPart
end
end
end
end
return best
local players = {
{ name = "Ada", x = 10, z = 0, health = 100 },
{ name = "Ben", x = 4, z = 3, health = 0 },
{ name = "Cy", x = -6, z = 8, health = 40 },
}
local best, bestDistance = nil, math.huge
for _, p in players do
if p.health > 0 then
local d = math.sqrt(p.x ^ 2 + p.z ^ 2)
if d < bestDistance then
best, bestDistance = p.name, d
end
end
end
print(best, bestDistance)
Ada 10In the cell, Ben is closest (5 studs). Why does the drone choose Ada?
Answer
Ben's health is 0, so he is skipped. Only living players are targets. Cy is 10 studs away too, but Ada was found first and Cy is not strictly closer, so Ada stays.
Open this step in lesson mode · Link to this step · This confused me
4Why the drone does not use Touched
You might expect the drone to hurt players with Touched, like the coins. It would be unreliable. Touched fires only for contact made by physical movement, and at least one of the two parts must be unanchored. Setting a CFrame, which is what PivotTo does, never fires it, even when the part ends up overlapping another2. A player who runs into the drone could still fire it, because their character moves by physics. A player standing still, caught by the drone, never would. A hit that lands only half the time is a bug, so touchNearby measures instead: a player is hit when their root is within 3.4 studs on the ground plane and less than 6 studs above or below.
local TOUCH_RADIUS = 3.4
local function touchNearby(drone: Drone)
for _, player in Players:GetPlayers() do
local character = player.Character
local rootPart = if character then character:FindFirstChild("HumanoidRootPart") else nil
if character and rootPart and rootPart:IsA("BasePart") then
local offset = rootPart.Position - drone.root.Position
local flat = Vector3.new(offset.X, 0, offset.Z)
if flat.Magnitude <= TOUCH_RADIUS and math.abs(offset.Y) < 6 then
applyTouch(drone, character)
Server sideRemember: The Touched event is a bump sensor.
Someone changes STOP_DISTANCE from 3 to 4 and leaves TOUCH_RADIUS at 3.4. What happens when a drill has one stationary target?
local STOP_DISTANCE = 4
local TOUCH_RADIUS = 3.4
Answer
The drone follows a lone stationary player but stops outside hit range. The drone stops 4 studs away, outside the 3.4-stud hit radius. Only a player who walks into it gets hurt. The two numbers are a contract: stop inside the radius.
Open this step in lesson mode · Link to this step · This confused me
5Damage, cooldown and the shield
applyTouch has its own per-player guard. drone.hits[player] stores the next time that player may be hit, DRONE_HIT_COOLDOWN (1 second) ahead. A player with a shield charge loses the charge instead of health. Otherwise the server calls Humanoid:TakeDamage(15).
local now = workspace:GetServerTimeNow()
local readyAt = drone.hits[player]
if readyAt ~= nil and now < readyAt then
return
end
drone.hits[player] = now + Config.DRONE_HIT_COOLDOWN
if shieldCharges(player) > 0 then
player:SetAttribute("ShieldCharges", 0)
feedback(player, "toast", "Shield broke")
return
end
humanoid:TakeDamage(Config.DRONE_DAMAGE)
feedback(player, "hurt", nil)
TakeDamage respects a ForceField. That is why MapBuilder sets the spawn's Duration to 0: a fresh character used to carry a 10-second shield and the drone could not hurt it.
A player with ShieldCharges = 1 is touched by the drone twice, 1.5 seconds apart. What happens?
Answer
The first touch breaks the shield, the second deals 15 damage. The first hit sets ShieldCharges to 0 and toasts Shield broke. 1.5 seconds later the cooldown has passed and there is no charge, so TakeDamage(15) runs.
Open this step in lesson mode · Link to this step · This confused me
6Stun: a timestamp, not a timer
A wrench hit calls applyDamage, which stuns the drone by writing a StunnedUntil attribute 1.6 seconds in the future. stepDrone checks it every frame: while stunned, the drone is painted white, outlined by a Highlight, and does not move. Nothing has to "end" the stun; time simply passes it.
local function stepDrone(dt: number)
local drone = current
if not active or drone == nil or drone.model.Parent == nil then
return
end
local stunned = isStunned(drone.root)
paint(drone, stunned)
if not stunned then
moveDrone(drone, dt)
end
touchNearby(drone)

Select a numbered pin, or a row below, to read what it marks.
The drone root on the server. Health and StunnedUntil are attributes, so clients can read them too.
Read stepDrone. Can a stunned drone still hurt a player who walks into it?
Answer
Yes; touchNearby runs whether or not the drone is stunned. Only moveDrone is skipped while stunned. touchNearby still runs, so walking into a frozen drone still hurts. That is a design choice you could change.
Open this step in lesson mode · Link to this step · This confused me
7Where the bump sensor breaks
The coins lesson called Touched a bump sensor. The drone is where that picture stops working. Answer first.
Where does it break?“A bump sensor” is a good picture of the touched event. Where does it stop being true?
It breaks here. It fires many times for one contact, and a part moved by setting its CFrame (PivotTo) makes no bump by moving, so a drone that lands on a player standing still never fires it.
The true model. Touched fires when the physics engine sees two parts start touching through simulated movement, and at least one of them must be unanchored. It fires per part, repeatedly, and not when a script sets a CFrame so that parts overlap. Gameplay needs its own guard, and moving hazards often use a distance check instead.
Why is a server distance check a good replacement for Touched here? Pick every true reason.
Answer
Moving the drone with PivotTo never fires Touched, so a player standing still would never be hit; The server owns the anchored drone's position and runs the check itself, so a client cannot move the drone or forge Touched events; It runs once per frame, so the hit cooldown still matters. Moving the drone never fires Touched (only the player's own movement could), the server owns the anchored drone's position and runs the distance check itself (each client still simulates its own character's HumanoidRootPart position, so movement sanity checks remain a separate defence), and the per-player cooldown stops a hit every frame. The check does cost a little per player per frame; with a handful of players it is tiny.
Open this step in lesson mode · Link to this step · This confused me
8Build it: add the drone
Other ways pros do this
| Option | What it is | When pros pick it | Trade-offs |
|---|---|---|---|
| Anchored model moved with PivotTo | The server sets the CFrame every Heartbeat. | Flying or hovering enemies, platforms and anything that must not be pushed. What Beacon Yard uses. | No physics: it passes through walls unless you add raycasts, and Touched does not fire. |
| Physics movers | Unanchored parts driven by constraints such as AlignPosition or LinearVelocity. | Things that should collide, bounce and be pushed. | A nearby client may become the network owner and can move it; the server can keep ownership with SetNetworkOwner(nil). |
| Humanoid NPC | A rig with a Humanoid, moved with MoveTo and PathfindingService. | Walking enemies that must go around obstacles, like the crawler later in this module. | Humanoids cost more per NPC than a plain part. |
Build along
src/server/DroneService.luausrc/server/RoundService.luausrc/server/init.server.luausrc/shared/Config.luau
In the game repo, run
git diff --stat m05-the-round m06-drone-wrench -- src/server/DroneService.luau src/shared/Config.luau.You should see DroneService.luau is new (about 440 lines) and Config.luau gains DRONE_SPEED, DRONE_DAMAGE, DRONE_HIT_COOLDOWN, DRONE_MAX_HEALTH and STUN_SECONDS.
Run
git diff m05-the-round m06-drone-wrench -- src/server/RoundService.luau.You should see RoundService calls options.setDroneActive(true) in runRound after the gates open, and setDroneActive(false) in Cooldown.
Check out
m06-drone-wrench, runrojo serve, connect Studio, press Play and start a drill.You should see No drone in Idle. During a drill a drone appears near (0, 4, 18), chases you, and each catch takes 15 health.
From the server command bar, run
game.ServerScriptService.RunGameTests:Invoke().You should see Output prints TEST PASS drone_damage.
Open this step in lesson mode · Link to this step · This confused me
9Challenges
Level 1, Apprentice
Challenge
Halve the drone's speed and feel the difference, then restore it.
- Start from
- The game at milestone m06 (download)
Done means
- With DRONE_SPEED at 6 you can outrun the drone at walking speed
- DRONE_SPEED is 12 again afterwards
Hints
- Players walk at 16 studs per second by default.
- DRONE_SPEED is in Config.luau.
Level 2, Builder
Challenge
Make the stun protect players too: a frozen drone deals no damage.
- Start from
- The game at milestone m06 (download)
Done means
- A stunned drone does not hurt players
- An unstunned drone still hurts as before
- drone_damage still passes
Hints
- Look at where touchNearby is called in stepDrone.
- One line moves; nothing else needs to change.
You raise HOVER_Y from 4 to 12. What happens to drone hits on a player standing on the ground?
Answer
They stop, because the height difference is over 6 studs. A character's root sits about 3 studs above the floor. At HOVER_Y 12 the difference is about 9 studs, more than the 6-stud limit, so no hit lands.
Level 3, Professional
Challenge
Make a stunned drone retreat, then chase again.
- 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 rungame.ServerScriptService.RunGameTests:Invoke("drone", "drone-retreat")in the server command bar. Copy the pass code from Output.
Done means
- While stunned, the drone backs away from its target instead of freezing
- After the stun ends, it chases again
- The default suite still passes: wrench_kill keeps its health checks (100, 66, 32, then gone) even though the drone now moves while stunned
- challenges/drone.luau prints TEST PASS for stun_retreat and chase_resumes
Hints
- Add a retreat step beside moveDrone: same hover height and speed, pointed away from the nearest living root. Call it instead of moveDrone while stunned, and keep touchNearby after it.
- A retreating drone breaks the suite's wrench_kill, which swings from one spot. Update that test so the player follows the drone before each swing, and keep its health checks; read challenges/solutions/drone.md 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 bump sensor”. Touched fires when the physics engine sees two parts start touching through simulated movement, and at least one of them must be unanchored. It fires per part, repeatedly, and not when a script sets a CFrame so that parts overlap. Gameplay needs its own guard, and moving hazards often use a distance check instead.
Recap
- The server moves the anchored drone on Heartbeat with PivotTo, DRONE_SPEED × dt per frame, at a fixed hover height.
- It targets the nearest living player and stops 3 studs short, inside the 3.4-stud hit radius.
- Moving an anchored part by CFrame never fires Touched, so damage is a server distance check with a per-player cooldown; stun is a timestamp attribute.
The Touched event is a bump sensor, until it fires many times for one contact, and a part moved by setting its CFrame (PivotTo) makes no bump by moving, so a drone that lands on a player standing still never fires it.
Queued for review
- DRONE_SPEED is 12 studs per second. At 60 frames per second, how far does the drone move in one frame, in studs?
- Someone changes STOP_DISTANCE from 3 to 4 and leaves TOUCH_RADIUS at 3.4. What happens when a drill has one stationary target?
- A player with ShieldCharges = 1 is touched by the drone twice, 1.5 seconds apart. What happens?
Open this step in lesson mode · Link to this step · This confused me