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

How can I make a block delete itself when touched + Play a sound?

Asked by 3 years ago

I'm trying to make a few animated jumpscares but for that to be properly complete I'll need to add sound once the animation is triggered which I don't know how to do and how I can make the block delete itself to prevent being triggered again. Anyone got a solution or good alternatives?

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago
local players = game:GetService("Players")
local part = workspace.Part
local sound = part.Sound

local triggered = false

part.Touched:Connect(function(hit) --detects if ANYTHING hits it, what you see in the functions paranthesis is what touched it.

    if players:GetPlayerFromCharacter(hit.Parent)  and triggered == false then --this line here makes sure that whatever touched the part is a player, not an npc or any other inanimite object.
        triggered = true --this sets the variable triggered to true because that will stop the sound from spamming due to roblox's touch event being a little bit weird.
        sound:Play()
        part:Destroy()
    end
end)

looks self-explanatory, if you want to understand how it works then comment

0
Okay, I'm new to roblox developing, if isn't a bother can you please go more in depth? ParaXero 14 — 3y
0
basically, when the trigger is touched, it checks if its already been triggered and that the part that touched it belongs to a player, and then sets the triggered variable to true to prevent from being triggered again. It then tells the sound object to begin playing, and then destroys the trigger. One thing about what you asked for is that it wont be able to ever be triggered again. Turtleraptor372 5 — 3y
Ad

Answer this question