Sound Activation but accurate

I’m a newish scripter, and I’m trying to make a sound for when you’re inside a transparent part. I need it to be very accurate as when I tested it, a person can have a arm sticking into a wall, and activating the sound like this.


I’m not sure how to make it very accurate.

Do you want it to activate when the entire player is inside the transparent part?

maybe not the entire player, as the opposite may happen. Maybe the torso, and the legs if that’s more accurate.

Try this:

--//Services
local Players = game:GetService("Players")

--//Variables
local Part = workspace.Part
local Sound = Part.Sound

--//Functions
Part.Touched:Connect(function(hit)
	--//Checks if the part hit isn't named Torso and isn't named HumanoidRootPart
	if hit.Name ~= "Torso" and hit.Name ~= "HumanoidRootPart" then
		
		--//If so, return
		return
	end
	
	--//Finds the player
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	
	--//If there isn't a player, return
	if not Player then
		return
	end
	
	--//Play the sound
	Sound:Play()
end)

It works much better now! The only problem is, the sound doesn’t stop when you exit the area.

Oops, didn’t know it’s supposed to stop. Here:

--//Services
local Players = game:GetService("Players")

--//Variables
local Part = workspace.Part
local Sound = Part.Sound

--//Functions
Part.Touched:Connect(function(hit)
	--//Checks if the part hit isn't named Torso and isn't named HumanoidRootPart
	if hit.Name ~= "Torso" and hit.Name ~= "HumanoidRootPart" then

		--//If so, return
		return
	end

	--//Finds the player
	local Player = Players:GetPlayerFromCharacter(hit.Parent)

	--//If there isn't a player, return
	if not Player then
		return
	end

	--//Play the sound
	Sound:Play()
end)

Part.TouchEnded:Connect(function(hit)
	--//Checks if the part's that hit ended isn't named Torso and isn't named HumanoidRootPart
	if hit.Name ~= "Torso" and hit.Name ~= "HumanoidRootPart" then

		--//If so, return
		return
	end

	--//Finds the player
	local Player = Players:GetPlayerFromCharacter(hit.Parent)

	--//If there isn't a player, return
	if not Player then
		return
	end

	--//Stop the sound
	Sound:Stop()
end)

Thank you so much. Works perfectly!

1 Like

No problem, if you have any more questions, feel free to ask. Have a good day!