Have you ever wanted to create a dangerous obstacle in your Roblox game? Maybe a pool of lava, a spike trap, or an electrified fence? In this tutorial, you’ll learn how to make a part that damages players when they touch it. This is a fundamental skill that opens up tons of possibilities for creating challenging and exciting gameplay!
What You’ll Need
Before we start scripting, let’s set up our workspace:
- A part in your workspace (this will be your damaging object)
- A Script inside that part (not a LocalScript!)
Go ahead and insert a part into your game. You can customize its appearance later—make it red for fire, green for toxic waste, or any theme you like! For now, let’s focus on making it work.
Understanding the Basic Concept
Here’s how our damage system will work:
- Detect when something touches our part
- Check if that something is a player’s character
- Find the player’s Humanoid (which controls health)
- Reduce the Humanoid’s health
Roblox makes this possible through something called the Touched event. Every part has this event, and it fires whenever another part comes into contact with it. Think of it like a doorbell—every time something “rings” (touches) your part, your code runs!
Writing the Script
Insert a Script (not a LocalScript) into your part. Here’s the complete code:
local part = script.Parent
local damage = 10
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(damage)
end
end
part.Touched:Connect(onTouch)
Let’s break down what each part does!
Getting the Part
local part = script.Parent
This line gets a reference to the part that contains our script. script.Parent means “the thing that holds this script,” which is our damage-dealing part.
Setting the Damage Amount
local damage = 10
Here we create a variable to store how much damage the part deals. Setting it as a variable at the top makes it easy to adjust later. Want a more dangerous obstacle? Just change this number!
The Touch Detection Function
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(damage)
end
end
This is where the magic happens! When something touches our part, this function runs. The otherPart parameter is automatically passed in by Roblox—it’s whatever part touched ours.
But here’s the thing: otherPart might be a player’s foot, arm, or head—it’s just a single body part, not the whole character. The actual character model (which contains the Humanoid) is the parent of that body part. That’s why we use otherPart.Parent.
We use FindFirstChild("Humanoid") to look for a Humanoid in that parent. Why? Because not everything that touches our part will be a player! It might be a random block, a ball, or another part. Only characters have Humanoids, so this is how we filter out non-players.
The if humanoid then check ensures that we found a Humanoid before trying to damage it. If we didn’t find one (meaning a non-player object touched us), the code inside the if statement simply won’t run.
Finally, humanoid:TakeDamage(damage) reduces the player’s health by the amount we specified. Simple and clean!
Connecting the Event
part.Touched:Connect(onTouch)
This final line connects our function to the part’s Touched event. Now, whenever anything touches the part, our onTouch function will run automatically. Think of Connect as plugging in a device—it hooks up our function so it actually works!
Testing Your Damage Part
Hit the Play button and walk your character into the part. You should see your health decrease! If it’s not working, double-check these common issues:
- Make sure you used a Script, not a LocalScript
- Verify the script is inside the part you want to be dangerous
- Check that
CanCollideis enabled on your part (otherwise players will walk right through it)
Customization Ideas
Now that you have the basics working, try these variations:
- Instant kill: Change
damageto 100 or higher - Gentle damage: Set
damageto 5 for a less punishing obstacle - Visual feedback: Add a bright color or particle effects to warn players of danger
- Make it invisible: Set
Transparencyto 1 for a sneaky trap!
Recap
Congratulations! You’ve just created a functioning damage system. You learned how to use the Touched event, check for Humanoids, and apply damage to players. These concepts form the foundation for countless game mechanics—from obstacle courses to combat systems.
The key takeaways are: the Touched event detects contact, FindFirstChild safely checks for Humanoids, and TakeDamage modifies player health.
What to learn next: Try adding a cooldown system so players don’t take damage multiple times per second, or explore healing parts that restore health instead of removing it. You could also learn about debounce techniques to make your damage system more controlled and professional!