Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Trying to make a jumpscare for my horror game the script wont work can u help me?

local part = script.Parent
local dummy = game.Workspace["Lost Soul"]
local sound = script.Parent["Scary Jumpscare"]

function Jumpscare()
	sound:Play()
	wait(1)
	dummy:Destroy()
end

part.Touched:Connect(Jumpscare)


1 comment

0

Can you confirm that the player touches the 'part'?

1 answer

When the part is touched, it is not specified what actually touched it.

For example,

local Part = game:GetService("Workspace"):WaitForChild("Part")

Part.Touched:Connect(function(TouchedPart)
	
	--This will fire every time the part is touched even if it isn't the player
	
end

We need to make sure its the Player touching the part and not some other random BasePart.

To ensure its the player we can do this:

local Players = game:GetService("Players")
local Part = game:GetService("Workspace"):WaitForChild("Part")

Part.Touched:Connect(function(TouchedPart)
	
	if Players:GetPlayerFromCharacter(TouchedPart.Parent) then
		
		--Do your code here
		
	end
	
end

The reason this works is because GetPlayerFromCharacter() will return Nil if no Player can be associated with the Character, thus meaning an If-Then Statement will return false if no player can be found.

This discussion is preserved as it was. New answers, comments, and votes are closed.