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

Script that makes a block play a sound when you touch it, but works only once?

How do I make a script that plays a sound when you touch the block but the next time you touch the block it doesn't play the sound?

2 answers

Assuming you know some stuff I wrote this script. So what this does is when a part is touched it will check if it was touched by a player and then play the sound. The script checks if the sound is playing already so it doesn't glitch. Then it waits and destroys the script so it can't run again.

local part = script.Parent --player will touch the parent of this script
local sound = script.Parent:WaitForChild("NameOfYourSound") --the sound is inside the part

part.Touched:connect(function(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human then
		if sound.IsPlaying == false then
			sound:Play()
			wait(time of sound)
			script:Destroy()
		end
	end
end)

1 comment

0

Honestly, we don't like to asnwer these questions. Mainly because its a non-attempt and they haven't shown anything they have tried.

Is this it?

local NotTouched = true
local Object = (insert part object here)
local Sound = (insert sound object here)
Object:Touched:connect(function(hit)
if hit == game.Players.LocalPlayer.Character.Humanoid and NotTouched then
NotTouched = false
Sound:Play()
end
end)
Object:TouchEnded:connect(function(hit)
if hit == game.Players.LocalPlayer.Character.Humanoid and NotTouched then
Sound:Stop()
end
end)

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