Conditions and events
Games react to players: when someone touches a part, something happens. For that you need events and if checks.
What you’ll learn
- react to touches with Touched
- check with if whether a player touched it
- use else for the other case
Events: “when this happens, then …”
Touched fires as soon as something touches the part. With :Connect you say which function should run then.
if, then, else
Not only players touch parts – other blocks do too. With if you check whether the touching part belongs to something with a Humanoid (every character has one):
local part = workspace.MyPart
part.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
part.BrickColor = BrickColor.new("Lime green")
else
part.BrickColor = BrickColor.new("Really red")
end
end)Just like in the code adventure
Remember ❓ If front is clear … else …? That is exactly the same idea: the program checks and then decides.
🛠️ Try it
Build a platform that turns green when a player stands on it. Bonus: make it turn grey again after 2 seconds (task.wait(2)).
🧠 Mini quiz
How do you know a player touched the part?
Every character has a Humanoid – other blocks don’t.
When does the else part run?
else is the “otherwise” case – just like in the code adventure.
✓ Lesson done! On to the next one.
Roblox and Roblox Studio are trademarks of the Roblox Corporation. mahshid.de is not affiliated with Roblox. All links to Roblox lead to the official sites.