This guide shows you how to create a basic enemy in Roblox Studio that can detect and chase a player, using built-in tools and a simple script. You’ll end up with a functional NPC that patrols, senses the player, and deals damage on contact. No prior scripting experience is needed, but you should know how to navigate the Studio interface.
What You’ll Need
- Roblox Studio installed and a place open (any template works, but a flat baseplate is easiest).
- A player character (your own avatar) to test the enemy.
- Basic understanding of the Explorer and Properties panels.
Step-by-Step
- Open Roblox Studio and create a new place. Go to the Home tab, click New, and select a template like Baseplate. This gives you a flat surface to work on.
- Insert a model for the enemy. In the Explorer panel (usually on the right), right-click on Workspace, hover over Insert Object, and choose Model. Name it “Enemy” by selecting it and typing in the Properties panel.
- Add a body part. Right-click on the Enemy model, select Insert Object, and choose Part. This will be the visible body. In the Properties panel, set its Size to something like (2, 4, 1) to make it humanoid-ish. Set its Color to red for visibility.
- Add a Humanoid. Right-click on the Enemy model, Insert Object, and choose Humanoid. This gives the enemy health, walking speed, and the ability to be animated. You don’t need to configure it much, but you can set its MaxHealth to 100.
- Add a script. Right-click on the Enemy model, Insert Object, and choose Script. Rename it to “EnemyScript”. This is where the enemy’s behavior will live.
- Write the detection and chasing script. Double-click the script to open the code editor. Replace any default code with the following:
local enemy = script.Parent local humanoid = enemy:WaitForChild("Humanoid") local rootPart = enemy:WaitForChild("Part") -- adjust to your part's name local walkSpeed = 16 local detectionRange = 30 local chaseRange = 50 while true do local players = game:GetService("Players"):GetPlayers() local nearestPlayer = nil local nearestDistance = detectionRange for _, player in ipairs(players) do local character = player.Character if character then local distance = (rootPart.Position - character:GetPivot().Position).Magnitude if distance < nearestDistance then nearestDistance = distance nearestPlayer = character end end end if nearestPlayer then humanoid:MoveTo(nearestPlayer:GetPivot().Position) else -- simple patrol: move to a random point nearby local randomPoint = rootPart.Position + Vector3.new(math.random(-20, 20), 0, math.random(-20, 20)) humanoid:MoveTo(randomPoint) end wait(0.5) endMake sure the part name matches the part you created (if you named it differently, change the line). This script makes the enemy chase the closest player within 30 studs, otherwise it wanders.
- Add damage on touch. To make the enemy hurt the player, you need a script inside the part. Right-click on the Part, Insert Object, choose Script, and name it "TouchDamage". Replace the code with:
local part = script.Parent local damage = 10 part.Touched:Connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid and humanoid.Health > 0 then humanoid:TakeDamage(damage) end end)Every time the part touches a player's body part, it deals 10 damage.
- Test the enemy. Click the Play button (or press F5) to enter test mode. Your character will spawn. Walk near the enemy and see if it starts moving toward you. Touch it to see if you lose health.
- Refine the enemy. You can adjust the walkSpeed, detectionRange, and damage values in the scripts to make the enemy harder or easier. You can also add more parts (like arms and legs) and weld them together for a better look.
Common Mistakes
- Wrong part name in script: If your part is not named exactly as in the script (e.g., "Part" vs "Body"), the script will error. Always check the name in Explorer.
- Forgetting Humanoid: Without a Humanoid, the enemy cannot move or take damage. Make sure you added it to the model.
- Script not running: If the script is inside the model but not in Workspace, it may not run. Ensure the model is in Workspace, and the script is inside the model.
- Using wrong pivot: In the script, I used GetPivot() for the player character. If your Roblox version is older, this might not work; use character.HumanoidRootPart.Position instead.
- Enemy stuck on obstacles: The enemy may get stuck on walls. You can set the Humanoid's JumpPower and MaxSlopeAngle to help it navigate.
Tips
- Use a ProximityPrompt if you want the enemy to only activate when the player presses a key, instead of always chasing.
- Add a HealthBar (a BillboardGui) to show the enemy's health.
- Make the enemy respawn by cloning the model when it dies, using a script in Workspace.
- For more advanced AI, look into PathfindingService to avoid obstacles smoothly.
- Use Animate script if you add a rig with animations, but for a simple part enemy it's not needed.
Troubleshooting
Enemy doesn't move at all: Check the Output window (View > Output) for script errors. Common issues: part name mismatch, missing Humanoid, or script not enabled. Also ensure the script is not disabled (Properties > Enabled).
Enemy moves but doesn't chase: The detection range might be too small. Increase detectionRange in the script. Also check that you are testing as a player (not in edit mode).
Player takes no damage: The TouchDamage script might be on the wrong part, or the part's CanTouch property is false. Ensure the part's CanTouch is true (default). Also check that the humanoid variable is correctly found; if your player character has a Humanoid, it should work.
Enemy falls through the floor: Make sure the part has Anchored = false (so it can fall) and the floor is a solid part. If the baseplate is not anchored, it might be moving.
Script errors about 'GetPivot': If your Roblox version doesn't support GetPivot, replace it with character.HumanoidRootPart.Position. Also ensure HumanoidRootPart exists.
Frequently Asked Questions
Can I make the enemy shoot projectiles? Yes, you can add a script that spawns a projectile part and gives it velocity toward the player. Search for "shooting script" in Roblox documentation.
How do I make the enemy only attack when close? In the chase script, add a condition to check distance before damaging. For example, only call MoveTo when distance < detectionRange, and damage only when distance < 5.
Can I use a free model enemy instead of building my own? Absolutely. Many free models on the toolbox include scripts. Just insert them, but be careful of viruses—use trusted creators.
How do I make the enemy respawn after death? Write a script in Workspace that listens for the enemy's Humanoid.Died event, waits a few seconds, then clones the original model and places it back.
Is it possible to make multiple enemies? Yes, just duplicate the Enemy model (Ctrl+D) and each will have its own script. They will all chase independently.
How do I make the enemy walk slower or faster? Change the walkSpeed variable in the EnemyScript. You can also set it per-humanoid in Properties.