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!
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)
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)
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)
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)
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.
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)
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.
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