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
Step 1 of 10
Something 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?
Press 1 to 4 to choose, Enter to check.
Step 2 of 10
One 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?
Step 3 of 10
Pick 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
In the cell, Ben is closest (5 studs). Why does the drone choose Ada?
Press 1 to 4 to choose, Enter to check.
Step 4 of 10
Why 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
Press 1 to 4 to choose, Enter to check.
Step 5 of 10
Damage, 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?
Press 1 to 4 to choose, Enter to check.
Step 6 of 10
Stun: 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?
Press 1 to 4 to choose, Enter to check.
Step 7 of 10
Where 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.
Press 1 to 4 to choose, Enter to check.
Step 8 of 10
Build 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.
Step 9 of 10
Challenges
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
Meet the criteria above, then record your own result. No test grades this check.
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
Meet the criteria above, then record your own result. No test grades this check.
You raise HOVER_Y from 4 to 12. What happens to drone hits on a player standing on the ground?
Press 1 to 4 to choose, Enter to check.
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
Step 10 of 10
Recap
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?