Buggy Audio Script [HELP]

It’s a rain script but for some reason the audio is really buggy and sounds so weird. Any clue why it’s doing this?:

-- Services --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

-- Starter Part --
local starterPart = Workspace:WaitForChild("StarterPart2")

-- Configuration --
local sizeX = 100  -- Size of the grid on X-axis (100 studs)
local sizeZ = 100  -- Size of the grid on Z-axis (100 studs)
local rainHeight = starterPart.Position.Y  -- Set to the Y position of StarterPart2
local loadedPositions = {}

-- Rain Generator --
local rainGenerator = Workspace:WaitForChild("RainGenerator")

-- Function to manage particle and sound cycles
local function manageRainCycle(emitter, sound)
	task.spawn(function()
		while true do
			-- Enable particle emission and set sound volume to 1
			emitter.Enabled = true
			sound.Volume = 1
			if not sound.IsPlaying then
				sound:Play()  -- Ensure the sound is playing
			end
			task.wait(10)  -- Active for 10 seconds

			-- Disable particle emission and set sound volume to 0
			emitter.Enabled = false
			sound.Volume = 0
			task.wait(10)  -- Inactive for 10 seconds
		end
	end)
end

-- Generate Rain Generator Model --
local function generateRainGeneratorModel(x, z)
	local position = Vector3.new(x * sizeX, rainHeight, z * sizeZ)

	-- Clone the RainGenerator
	local clone = rainGenerator:Clone()
	clone.Parent = Workspace
	clone.Transparency = 0  -- Make it visible
	clone.Position = position

	-- Find the existing ParticleEmitter in the RainGenerator
	local emitter = clone:FindFirstChildOfClass("ParticleEmitter")
	if emitter then
		-- Add sound to the RainGenerator
		local sound = Instance.new("Sound")
		sound.SoundId = "rbxassetid://9112854625"  -- Assign the sound ID
		sound.Looped = true  -- Sound will loop continuously
		sound.Volume = 0  -- Start with the sound muted
		sound.Parent = clone  -- Parent the sound to the RainGenerator

		-- Start the synchronized rain cycle
		manageRainCycle(emitter, sound)
	end
end

-- Generate Rain Grid --
local function generateRainGrid()
	for z = -25, 25 do  -- Adjust range to match the grid size (50x50 grid)
		for x = -25, 25 do
			local key = tostring(x) .. "_" .. tostring(z)
			if not loadedPositions[key] then
				generateRainGeneratorModel(x, z)
				loadedPositions[key] = true
			end
		end
	end
end

-- Start Generating the Rain Grid --
generateRainGrid()

Any Ideas? Thanks!

1 Like

My guess would just be lag between each sound being played and particle being emitted, right now you have between 448 to 2601 parts being used and a sound being played at each 10 second interval. I highly recommend limiting “RainGenerator”(s) in the background as some cant even be heard or seen.

You can create a grid system with a radius from the player’s coordinates to activate any generators within that radius and ignore/disable the ones outside that radius.

2 Likes

Correct, or you can decrease the number of sounds by a factor and then space them evenly in the for loop.

E.g

for i=1,1000 do
    if i % 50 == 0 then  
        -- create sound every 50th iteration
    end
end
1 Like

Yes however this makes the sound more inconsistent depending on how spaced out you want your rain to be, neat idea though I’m sticking to my idea to removing super-distant noises hehe.

Roblox has a sound limit no matter how hard you try to bypass it. Also if you have tons of rain drops I assume it would start to sound bad. Most games will just decrease the number of rain drop sounds to a point where you can’t really distinguish each drop, but can distinguish it at the rate of how many drops are per second.

1 Like