Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
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)


0
Can you confirm that the player touches the 'part'? appxritixn 2235 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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.

Ad

Answer this question