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

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

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

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

2 comments

0

Okay, I'm new to roblox developing, if isn't a bother can you please go more in depth?

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.

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