Splash sound looping when camera enters water

Hi, I’m pretty new to programming, and I need help patching a bug in a script. The script is supposed to play a splash sound once the camera enters water, but it loops constantly.

Here’s the script I wrote:

local Work = game:GetService(“Workspace”)
local Camera = Work.CurrentCamera
local Terrain = Work.Terrain

function CFrameToVector3(cframe)
return Vector3.new(cframe.x,cframe.y,cframe.z)

while wait (0.1) do
local REIGON = Terrain:ReadVoxels(Region3.new(CFrameToVector3(Camera.CFrame)-Vector3.new(.00001,.00001,.00001), CFrameToVector3(Camera.CFrame)):ExpandToGrid(4),4)
for Number,Information in pairs(REIGON[1][1]) do
for i,v in pairs(Work:GetDescendants()) do
if Information == Enum.Material.Water and v:IsA(“Sound”) and v.Name == “Sploosh” then
v.Playing = true
elseif v:IsA (“Sound”) and v.Name == “Sploosh” then
v.Playing = false

The sound I used (being “Sploosh”) wasn’t looped. I did use a guide which I heavily modified, but I’m still not sure why it loops. Thanks for reading.

You should have a bool to detect if the sound was already played since you’re checking it in a loop and only running code when the result is different.

Ex: bool is false
Camera enters water
if bool is false then —yes it is false
bool = true
—code here

And vice versa

1 Like