Jumpscare not working

So I am currently trying to make a jumpscare, but for some reason it does not work. Here is my code:

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

local JumpscareGui = script.Parent
local Jumpscare = JumpscareGui:WaitForChild("Jumpscare")
local Sound = script:WaitForChild("Sound")

local debounce = false

TouchPart.Touched:Connect(function(hit)
	if hit.Parent.Name == "Character" then
		if debounce then
		debounce = true
		print("debounce")
		
		Jumpscare.Visible = true
		Sound:Play()
		task.wait(3)
		Jumpscare.Visible = false
		print("You've been jumpscared")
		debounce = false
		end
	end
end)

If you think you know what went wrong, please let me know. Thank you and have a wonderful day! :slight_smile:

2 Likes

Is that local? If yes, then you don’t need “hit”.

If local, you can do this:

TouchPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
		if game.Players[hit.Parent.Name] == game.Players.LocalPlayer then
if debounce then
		debounce = true
		print("debounce")
		
		Jumpscare.Visible = true
		Sound:Play()
		task.wait(3)
		Jumpscare.Visible = false
		print("You've been jumpscared")
		debounce = false
		end
end
	end
end)

I think what is the mistake. Hit.Parent.Name doesn’t exist to be “Character” it’s the model’s name

So, if server:

TouchPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce then
		debounce = true
		print("debounce")
		
		Jumpscare.Visible = true
		Sound:Play()
		task.wait(3)
		Jumpscare.Visible = false
		print("You've been jumpscared")
		debounce = false
		end
	end
end)
3 Likes

It is local. I copy and pasted your script but sadly it didn’t work.

Debounce doesn’t start as true, therefore your main script never runs. You also need to check if the player who touched is the LocalPlayer.

Code:

--//Services
local Players = game:GetService("Players")

--//Variables
local LocalPlayer = Players.LocalPlayer
local TouchPart = workspace:WaitForChild("TouchPart")
local JumpscareGui = script.Parent
local Jumpscare = JumpscareGui:WaitForChild("Jumpscare")
local Sound = script:WaitForChild("Sound")

--//Controls
local debounce = false

--//Functions
TouchPart.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if not player or player ~= LocalPlayer or debounce then
		return
	end
	
	debounce = true
	print("debounce")

	Jumpscare.Visible = true
	Sound:Play()
	task.wait(3)
	Jumpscare.Visible = false
	
	print("You've been jumpscared")
	debounce = false
end)
1 Like

You should search for a humanoid, instead of checking it the hit instance’s name is Character, also you checked if the debounce was true then made it true? Which im guessing was a mistake, so i fixed that. theres also no need for task.wait() when you can use task.delay() in this case. Make sure to get back to me if this still doesnt work, but it should work.

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

local JumpscareGui = script.Parent
local Jumpscare = JumpscareGui:WaitForChild("Jumpscare")
local Sound = script:WaitForChild("Sound")

local debounce = false

TouchPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") and not debounce then
			debounce = true
			print("debounce")

			Jumpscare.Visible = true
			Sound:Play()
			
		task.delay(3, function()
			Jumpscare.Visible = false
			print("You've been jumpscared")

			debounce = false
		end)
	end
end)

Try to put debounce to false at the start like @Katrist said

task.delay is not needed for this function, because the function does not yield for anything. It just wastes more resources than task.wait() because it has to create a new artificial thread.

You also don’t account for if another player who isn’t the LocalPlayer steps on the part, because it will also fire.

TouchPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil and not debounce then
		if game.Players[hit.Parent.Name] == game.Players.LocalPlayer then
		debounce = true
		print("debounce")
		
		Jumpscare.Visible = true
		Sound:Play()
		task.wait(3)
		Jumpscare.Visible = false
		print("You've been jumpscared")
		debounce = false
		end
	end
end)

Try this one

Why would you need to tell him twice?

1 Like

I putted the debounce now and fixed it

1 Like

theres no need for

game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil

if theres already a humanoid unless you truly only want it to be player based, which really doesnt matter, also had no reason to check twice if it was player, you also repeated the same mistake as the person asking.

2 Likes

Ok. Then it should be like this:

TouchPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and  not debounce then
		if game.Players[hit.Parent.Name] == game.Players.LocalPlayer then
		debounce = true
		print("debounce")
		
		Jumpscare.Visible = true
		Sound:Play()
		task.wait(3)
		Jumpscare.Visible = false
		print("You've been jumpscared")
		debounce = false
		end
	end
end)

this is correct, although it could be shortened to look better

Yes. Also, y’all don’t forget that this is inside the GUI.

Yes. But it still works

You do realize you can use :GetPlayerFromCharacter instead of that if statement right?

That’s exactly what I did. But @lgotanintendoswitch said that it was better without it
And he was right

He said it was better without checking twice. The problem was that you checked if the value was there twice instead of once, which is not very efficient.

Ok? But it still works. That’s what is important for @Kittylitterking123
It’s to work

2 Likes

Once again, in my reply i said if you wanted it to be absolutely player based then you could do what Karil implied, read properly next time

1 Like

You have to do if not debounce then im pretty sure
also check if the hit’s parent is the player’s character directly by doing if hit.Parent == game.Players.LocalPlayer.Character then