Beginner Tutorial #2: How To Make A Jumpscare!

Hello!

A lot of you seemed to really enjoy my previous tutorial: Beginner Tutorial #1: How To Make a Part or NPC Chat! - Resources / Community Tutorials - Developer Forum | Roblox so here I am again teaching you how to make a jumpscare upon touching a part! Enjoy! :slight_smile:

Step 1: Create a part and name it “TouchPart”.

Step 2: Inside of StarterGui, create a new ScreenGui named “JumpscareGui” and insert an ImageLabel. Scale it to your liking and set the Image property to the ImageID of the image you want to appear. Set Visible to false.

Step 3: Create a LocalScript parented to your ScreenGui and name it “JumpscareHandler”. (This next part is optional.) Parent a new Sound object to your script with the id of the sound you want to play when the jumpscare is shown. Name the Sound “Jumpscare Sound.”

Your hierarchy should look like this:
image

Step 4: Open up your script and delete the default code. First, we will need a couple of variables. We need the Player object and the TouchPart which should be parented to workspace. If you don’t know how to get the Player, let me show you.

local Player = game.Players.LocalPlayer

The Player can only be accessed this way by a LocalScript, not a regular server script.

Next, we need the part.

local TouchPart = game.Workspace:WaitForChild("TouchPart")

This line uses the WaitForChild method, which basically just yields the script until the instance within the quotation marks exists in the game. So what this does is it waits until TouchPart is loaded in and then it will run the rest of the code below it.

Now we need the Jumpscare Imagelabel and the JumpscareSound if you have one.

local Jumpscare = script.Parent.Jumpscare
local JumpscareSound = script:WaitForChild("JumpscareSound", 5)

Notice the number 5 after the end quotation mark. That is because the WaitForChild function takes two parameters:

1: The instance to wait for
2, The amount of time to wait.

So here we just telling the script to wait five seconds for the sound to be loaded in. This parameter is optional. You can use it in the TouchPart variable if you want.

Last, we will need a debounce.

local debounce = false

A debounce is basically just a way to make sure the jumpscare can’t be triggered multiple times all at once. You will see how it is used as we write our script.

Step 5: Connect a function to TouchPart’s .Touched event.

TouchPart.Touched:Connect(function()
-- Code will go here
end

Step 6: Now we will use an if statement to check for a couple of things. We will check if the player does not exist and if it does, it is not equal to game.Players.LocalPlayer. Also, check if debounce does not exist. If none of these exist, use something called a guard statement to stop the function.

if not Player or Player ~= game.Players.LocalPlayer or debounce then return end

Quick note: A guard statement is a helpful thing to use in code and is commonly used with if statements. The if statement runs when a certain condition is met and the guard statement runs when a certain condition is NOT met. It will return the result (either true or false) and tell the function to stop running.

Now we will set our debounce to true and set up a print statement to make sure it debounces successfully.

debounce = true
print("debounced")

We will also make the Jumpscare Visible and play the sound.

Jumpscare.Visible = true
JumpscareSound:Play()

We don’t want the jumpscare to be on the screen forever, so we will wait three seconds and then set Jumpscare.Visible to false. I’ll set up a print statement to make sure it worked. Last, we will set our debounce back to false.

task.wait(3)
Jumpscare.Visible = false
print("You've been jumpscared")
debounce = false

Final code:

local Player = game.Players.LocalPlayer
local TouchPart = game.Workspace:WaitForChild("TouchPart")
local Jumpscare = script.Parent.Jumpscare
local JumpscareSound = script:WaitForChild("JumpscareSound", 5)

local debounce = false

TouchPart.Touched:Connect(function()
	if not Player or Player ~= game.Players.LocalPlayer or debounce then return end
	debounce = true
	print("debounced")
	Jumpscare.Visible = true
	JumpscareSound:Play()
	task.wait(3)
	Jumpscare.Visible = false
	print("You've been jumpscared")
	debounce = false
end)

Step 7: Playtest your code and see if it runs. Whenever you touch the part, your jumpscare should appear and your sound will play. After three seconds of being visible, it will disappear and you can go back to playing the game.

I hope you enjoyed this tutorial! If you did, please leave a like! If you have any questions, need help, or would like to know how to implement something, don’t hesitate to ask. I would love to help you! Have a wonderful day! :slight_smile:

10 Likes

Thx for this tutorial. I can see myself using this sometime in the close future

2 Likes

You’re welcome! I’m glad this helped you!

New tutorial out now: Beginner Tutorial #3: How To Make A Simple Shift To Sprint Script!