"

10 Events

Kyra Ma; Hasan Malik; and Rutwa Engineer

Learning Objectives

  • Learn about events and why they are useful for game interactivity.
  • Learn about the PlayerAdded and PlayerRemoving events.
  • Learn to connect functions to events using both inline and named functions.
  • Learn about the Touched and CharacterAdded events.

Introduction

What are Events?

Events are a fundamental part of game development in Roblox. They allow scripts to respond to actions like players joining, clicking buttons, or objects being touched. Events are useful because they enable scripts to communicate and react dynamically without running unnecessary loops.

Why are Events Important for Game Development?

  • Efficient Performance: Events run only when triggered, unlike loops that constantly check conditions
  • Improved Interactivity: They allow games to respond to player actions, such as jumping, clicking, or entering an area.
  • Script Communication: Events enable scripts to work together by sending and receiving signals

The Player Folder: game.Players

In Roblox, all players are stored in the game.Players service. This allows developers to track when a player joins or leaves the game, customize player settings, and trigger personalized experiences.

The PlayerAdded Event

  • The PlayerAdded event fires whenever a new player enters the game. This is useful for:
  • Welcoming players with a message
  • Assigning default stats or tools
  • Setting up player-specific game data

The PlayerRemoving Event

The PlayerRemoving event runs when a player leaves the game. It is useful for:

  • Saving player progress
  • Cleaning up game objects related to the player

Writing Event Functions

There are two common ways to define event-handling functions. In these examples, we will be welcoming new players into the game.

Inline Anonymous Functions

The function is defined directly inside Connect(), keeping it concise. It is suitable for simple, single-use cases.

game.Players.PlayerAdded:Connect(function(player)
print("A new player has joined into the game!")
print(player)
end)


This looks complicated, but don’t worry! Let’s break down the syntax:

game.Players.PlayerAdded:Connect(

  • game.Players → Refers to the Players service in Roblox, which manages all players in the game.
  • .PlayerAdded → An event that fires whenever a new player joins the game.
  • :Connect(…) → Connects a function to the PlayerAdded event so that the function runs whenever a player joins.

function(player)

  • This is an inline (anonymous) function, meaning it is not given a name and is defined directly inside Connect(…).
  • player → A parameter that represents the player who just joined.

print("A new player has joined into the game!")
print(player)

  • print(“A new player has joined into the game!”) → Outputs this message to the console when a new player joins.
  • print(player) → Prints the player object, which contains information about the joining player (such as their name).

Named Functions

If you coded other languages before, this format is much more familiar! The function is declared separately and then connected to the event. Useful when the function needs to be reused.

local function playerAdded(player)
print("A new player has joined")
print(player)
end

game.Players.PlayerAdded:Connect(playerAdded)


Breaking down the syntax:

local function playerAdded(player)

  • local → This keyword makes the function local, meaning it can only be used within the script where it is defined.
  • function playerAdded(player) → Defines a function named playerAdded, which takes one parameter, player. This parameter represents the player who just joined the game.

print("A new player has joined")
print(player)

  • print(“A new player has joined”) → Outputs this message to the console whenever a new player joins.
  • print(player) → Prints the player object. In Roblox, this will display information about the player, such as their name.

end

  • Ending the Function, the end keyword marks the conclusion of the function. This should appear automatically for you while coding!

game.Players.PlayerAdded:Connect(playerAdded)

  • game.Players → This refers to the Players service in Roblox, which manages all players in the game.
  • .PlayerAdded → This is an event that fires whenever a new player joins the game.
  • :Connect(playerAdded) → This connects the playerAdded function to the PlayerAdded event. Whenever a player joins, the function will execute, printing the welcome message and player information.
  • Essentially, this part  connects the Function to an Event!

2 Common Events in Roblox

Touched Event

The Touched event fires when a part is touched by another object, such as a player’s character or an NPC. This is useful for traps, teleporters, doors, or interactive objects.

Example: Basic object detection

local touchPart = game.Workspace.TouchPart

touchPart.Touched:Connect(function(otherPart)
print(otherPart.Name)
end)

In this example, when any object touches touchPart, the Touched function runs, which prints a message to track the part of the player that touched touchPart.

CharacterAdded Event

The CharacterAdded event fires when a player’s character spawns in the game world. This is useful for applying player-specific settings, adding health bonuses, or applying effects.

Debouncing!

Sometimes, an event may fire multiple times in quick succession. Debouncing prevents unintended behavior. This prevents multiple, rapid touch triggers from happening at the same time!

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)

What does this do?

local touchPart = game.Workspace.TouchPart

local partIsTouched = false

  • touchPart: This variable stores a reference to a part in the Workspace named TouchPart.
  • partIsTouched: This is a boolean flag that keeps track of whether the part has already been touched to prevent repeated triggers.

touchPart.Touched:Connect(function(otherPart)

  • The Touched event fires when another part collides (touches) touchPart.
  • .Connect(function(otherPart)): Attaches a function to run whenever the event occurs.
  • otherPart: The part that touched touchPart.

if partIsTouched == false then

  • Check if the part has already been touched.
  • Prevents the function from running multiple times until a cooldown resets it.

partIsTouched = true
print(otherPart.Name)

  • partIsTouched = true: Marks the part as touched, preventing re-triggering.
  • print(otherPart.Name): Displays the name of the part that touched touchPart in the output console.

task.wait(2)
partIsTouched = false

  • task.wait(2): Pauses the script for 2 seconds (cooldown).
  • partIsTouched = false: Resets the flag, allowing the function to trigger again.

Why is this system good?

It prevents spam triggering when multiple objects touch touchPart quickly. It also implements a cooldown system to limit how often the event can occur.

Exercises

      1. Detect When a Player Leaves

Modify the PlayerRemoving event to display a farewell message.

      2. Create a Door System

Use the Touched event to open a door when a player touches a button

      3. Health System with CharacterAdded

Print a player’s health every time their character spawns.

      4. Create a Toggle Button

Make a button that changes color each time it is clicked using ClickDetector.MouseClick.

Congratulations—you are now familiar with events in Roblox Studio!

 

 

 

 

 

 

License

Learn Coding and Animations in Lua Code Copyright © by Kyra Ma; Hasan Malik; and Rutwa Engineer. All Rights Reserved.