Touch Pad
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 11 of 24.
So far your scripts ran at the start. Games react to what players do, and Roblox tells you about it with events. A part's Touched event fires every time something bumps into it. You connect a function to it, and Roblox runs that function on every touch:
local bell = script.Parent
bell.Touched:Connect(function(hit)
print("Ding!")
end)The function gets hit: the part that did the touching. When a player walks onto the bell, hit is one of their body parts, like Left Leg (LeftFoot on newer avatars), so hit.Name tells you which one.
The function above is written right inside Connect(...) and has no name: it exists only to answer the event.
Challenge
MediumThe gray pad is waiting for a visitor, and this script is inside it. When something touches the pad:
- print the name of the part that touched it
- turn the pad green:
Color3.fromRGB(0, 255, 0)
Press Play and step on it, then Check.
Try it yourself
local pad = script.Parent
pad.Touched:Connect(function(hit)
-- 1. Print the name of the part that touched the pad
-- 2. Turn the pad green
end)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Roblox Game Scripting
2Loops and Functions
Row of PillarsStaircase BuilderTower BuilderCoin FactoryBlinking LightVanishing Bridge5Your First Game
Your First ObbyPractice on your own: Online Lua compiler