Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Parts, Physics and Building

Touched Events and Hazards

Use the Touched event to build kill bricks, coin pickups and checkpoints. Find the Humanoid and Player, add a debounce, handle many touches, and use GetPartsInPart as an alternative.

Many Roblox mechanics begin with "when something touches this." A lava floor that hurts you, a coin that vanishes when grabbed, a checkpoint that saves your spot. All of them use the Touched event. This lesson builds those three, explains why they sometimes fire dozens of times, and shows the debounce pattern that fixes it.

The Touched event

Every BasePart has a Touched event. It fires with the other part that touched it:

LUA
local pad = workspace.Pad

pad.Touched:Connect(function(otherPart)
	print(pad.Name, "was touched by", otherPart.Name)
end)

TouchEnded fires when they separate. Touch events only fire if CanTouch is true on the part, and the event needs movement: at least one of the two parts must be simulated (an unanchored part). A player's body is made of unanchored parts, so a walking player triggers it, while two anchored parts never do. A pad can be anchored.

The otherPart is usually a limb (a leg, arm, torso), not the player. To learn who touched it, look at the part's parent.

Finding the Humanoid and the Player

A character is a Model that contains a Humanoid. If the touching part's parent has a Humanoid, a character touched you:

LUA
local Players = game:GetService("Players")

local function getCharacterInfo(otherPart)
	local character = otherPart.Parent
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")
	if not humanoid then
		return nil
	end
	local player = Players:GetPlayerFromCharacter(character)
	return character, humanoid, player
end

GetPlayerFromCharacter returns the Player, or nil if the character is an NPC. Accessories such as hats are parts too; their parent is an Accessory, so the check above skips them, which is usually what you want. If you need to handle them, climb with otherPart:FindFirstAncestorOfClass("Model").

A kill brick

LUA
-- Script inside the lava part, or any Script that references it
local lava = workspace.Lava

lava.Touched:Connect(function(otherPart)
	local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid.Health = 0      -- instant death
	end
end)

A damage zone using TakeDamage:

LUA
spikes.Touched:Connect(function(otherPart)
	local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid:TakeDamage(10)
	end
end)

Humanoid:TakeDamage(n) respects force fields; setting Health directly does not. Players' characters start with 100 health and respawn when it hits zero.

The problem: it fires many times

Physics reports every limb that touches, every frame the contact changes. A character stepping onto spikes can trigger Touched five or ten times in a heartbeat, dealing 100 damage instead of 10. The cure is a debounce: a flag or timestamp that makes the handler ignore touches for a short time.

LUA
local DAMAGE = 10
local COOLDOWN = 1
local lastHit = {}    -- humanoid -> time of last hit

spikes.Touched:Connect(function(otherPart)
	local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
	if not humanoid then
		return
	end

	local now = os.clock()
	if lastHit[humanoid] and now - lastHit[humanoid] < COOLDOWN then
		return                      -- still cooling down: ignore
	end
	lastHit[humanoid] = now

	humanoid:TakeDamage(DAMAGE)
end)

Keeping the table keyed by humanoid means each character has its own cooldown. A single global debounce = true flag is simpler, but then one player's touch blocks everyone else for a moment. Remember to clean the table: players leave, and a leftover entry is a small memory leak. A common fix is to key by the character and remove the entry when the humanoid dies.

A coin pickup

A coin should give once and disappear. Flipping a flag first prevents double rewards:

LUA
local coin = workspace.Coin
local collected = false

coin.Touched:Connect(function(otherPart)
	if collected then
		return
	end
	local character = otherPart.Parent
	local player = game:GetService("Players"):GetPlayerFromCharacter(character)
	if not player then
		return
	end

	collected = true            -- set the flag BEFORE doing anything slow
	coin:Destroy()
	print(player.Name, "collected a coin")
end)

Set the guard before any task.wait or yielding call, or two touches can both slip through. Awarding the actual coins would use a leaderstat or a data module, which the data stores chapter covers.

A checkpoint

Save the player's latest checkpoint and use it when they respawn:

LUA
local checkpoints = {}  -- player -> part

for _, part in workspace.Checkpoints:GetChildren() do
	part.Touched:Connect(function(otherPart)
		local player = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
		if player then
			checkpoints[player] = part
		end
	end)
end

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local checkpoint = checkpoints[player]
		if checkpoint then
			task.wait()                                 -- let the character finish loading
			character:PivotTo(checkpoint.CFrame + Vector3.new(0, 4, 0))
		end
	end)
end)

Studio also has SpawnLocation objects with a Neutral/TeamColor setting, which handle many checkpoint cases without code.

When Touched is not enough

Touched is event-driven by movement, so it can miss a player standing still on a pad. Alternatives:

  • workspace:GetPartsInPart(zonePart) returns every part overlapping a zone right now. Poll it every half second for a healing field.
  • workspace:Raycast(origin, direction) checks what a line hits (ground checks, line of sight, guns).
  • workspace:GetPartBoundsInRadius(position, radius) finds parts near a point (explosions).
LUA
local zone = workspace.HealingZone
while true do
	for _, part in workspace:GetPartsInPart(zone) do
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.Health = math.min(humanoid.Health + 5, humanoid.MaxHealth)
		end
	end
	task.wait(1)
end

Duplicates are possible (every limb overlaps), so for exact rules collect humanoids into a set first.

Stand-in: Touched with and without debounce

The real engine fires Touched from physics. This JavaScript stand-in fires it several times in a row, as limbs overlap, to show why a debounce is needed.

JS Stand-in: damage with and without debounce (JavaScript)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Without a debounce the player took 50 damage from one step.

Stand-in: set the guard first

The coin example hinges on the flag being set before any waiting. This stand-in compares a guard set early (safe) with one set late (a race that double-pays):

JS Stand-in: guard flag before yielding (JavaScript)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Setting the flag early gives one payout; setting it late pays twice.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the Touched event pass to your function?

    Touched gives the other BasePart, often a limb. Look at its Parent to find the character.
  2. 2. Why does a kill brick often need a debounce?

    Several limbs and frames report contact, so the handler runs repeatedly unless you add a cooldown.
  3. 3. How do you find the Player from a touching part?

    The part's parent is the character model; GetPlayerFromCharacter maps it to the Player.
  4. 4. When should you use GetPartsInPart instead of Touched?

    GetPartsInPart checks overlap on demand, so it also sees things that are not moving.

Summary

  • part.Touched:Connect(function(other) ... end) runs when parts touch; it needs CanTouch and movement.
  • Climb from the touching limb to the character, the Humanoid and Players:GetPlayerFromCharacter.
  • Debounce with a cooldown table or flag, set before any yielding code.
  • Use GetPartsInPart, Raycast or GetPartBoundsInRadius when Touched is not reliable enough.

Next: Tweening and Moving Parts