Welcome back to my beginner’s guide to scripting in Roblox Studio. I’m Jaden, and in this guide, we’re diving into a super important topic — events. But before we get technical, let me walk you through a little story.
Imagine it’s your birthday. You’ve just finished a long day at school, you’re tired, walking home from the bus. You finally reach your front door, twist the knob, and as the door swings open — surprise! Your family and friends leap out yelling “Happy Birthday!” They shower you with hugs, gifts, and love. Now, here’s the interesting part: that whole party didn’t just happen. It was triggered the moment you opened the door.
That’s what events are like in Roblox Studio — actions triggered by something happening first.
Setting Up
First things first, head over to the script we created in the last episode and disable it. We’re starting fresh, so right-click the Workspace, insert a new Script, and rename it to something like Events
. Then just delete any starter code inside it. Clean slate.
Events in Roblox can seem complex — and they do get pretty deep — but today, I’ll introduce you to two really useful built-in events that are common and powerful: PlayerAdded
and Touched
.
1. The PlayerAdded
Event: Trigger When Someone Joins
This one is all about detecting when a new player enters the game. Up until now, we’ve been working with the Workspace
, but this event lives in the Players folder in Roblox’s game data model.
To set this up, go into your script and write:
game.Players.PlayerAdded:Connect(function(player)
print("A new player has joined the game!")
print(player)
end)
Notice how we’re not using Workspace
here — we’re tapping into game.Players
, and connecting an event to a function using :Connect()
. That function takes a player
parameter (you can name it whatever you like), which represents the player who just joined. Inside the function, we print a message and then print the player object itself.
When you hit Play, you should see your own player’s name pop up in the output, confirming the event was triggered.
Alternative Way to Write It
Prefer a more organized or reusable style? You can define the function first and then connect it, like this:
local function onPlayerAdded(player)
print("A new player has joined")
print(player)
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
Same effect, just a cleaner structure — especially helpful when your functions get longer or more complex.
2. The Touched
Event: When Players Collide With Parts
Let’s move into the physical world now — touching objects in the game. Every part in your Workspace, whether it’s the baseplate or a new object you insert, can detect when something touches it using the Touched
event.
So go ahead and insert a new Part into the Workspace. Rename it to TouchPart
, and make sure to anchor it so it doesn’t fall with gravity. You can move it up a little with the Move Tool to make it float visibly.
Back in your script, delete the previous code and let’s reference this new part:
local touchPart = game.Workspace.TouchPart
touchPart.Touched:Connect(function(otherPart)
print(otherPart.Name)
end)
This function runs every time something touches the part — and it prints the name of that “something.” If you hit Play and walk into the part, you’ll see a bunch of body parts being printed out like “UpperTorso”, “Head”, “Left Leg” and so on. That’s because your player is made up of multiple connected parts!
Add a Cooldown with Debouncing
Here’s a problem though — your player has many parts, so this event fires a lot when you touch the part. What if we only want it to fire once every few seconds, no matter how many body parts touch it?
That’s where debouncing comes in. It’s a simple way to add a cooldown.
local touchPart = game.Workspace.TouchPart
local partIsTouched = false
touchPart.Touched:Connect(function(otherPart)
if partIsTouched == false then
partIsTouched = true
print(otherPart.Name)
task.wait(2)
partIsTouched = false
end
end)
Now, the message only prints once every 2 seconds, no matter how many parts bump into it during that time. This is a powerful way to prevent spamming actions in your game.
Your Turn!
Now that you know how to use PlayerAdded
and Touched
, I encourage you to play around with them. Try making the part change color when a player touches it — maybe even change it back after a few seconds. Experiment with how you respond to players joining the game. Maybe send them a welcome message or give them starting items.
Once you’ve made something cool, drop your code in the comments of the video so others can see what you’ve built. It’s always great to share and learn from each other.
Thanks for hanging out and learning with me — hope this gave you a solid intro into events and how they power your games. I’ll see you in the next blog. Take care!