How to optimize the way my game loads water

  • What does the code do and what are you not satisfied with?
    Loads water into the game, with the intent of also using the blocks to provide “waterRegions” for other uses, like detecting if a player is in water.
    I am not satisfied with the performance of how the water loads
  • What potential improvements have you considered?
    I know there are other ways of going about this, but I specifically want to use parts to load water into the game to serve as more potent form of detecting water, rather than reading voxels of water terrain.
    I’ve tried manually making terrain water and putting parts in by hand, but I wanna just be able to use parts as its more precise when building.
  • How (specifically) do you want to improve the code?
    I want to improve the performance, and if possible get help with other solutions of conveniently loading water
	local WaterRollOffMultiplier = 2
	local function SetupWater()
		warn("-- Loading Water...This may take awhile.. --")
		local function getChildWithWarning(parent, childName, warnMessage)
			local child = parent:FindFirstChild(childName)
			if not child then 
				warn(warnMessage)
			end
			return child
		end
		local t = workspace:FindFirstChild("Terrain")
		if not t then
			warn("Couldn't get terrain folder!")
		else
			local WaterF = getChildWithWarning(t, "WaterTerrain", "Couldn't get water terrain folder!")
			if WaterF then
				local resources = WaterF:FindFirstChild("Sounds")
				if not resources then
					warn("Couldn't get sounds folder!")
				end
				resources.OceanWater.Playing = true
				resources.SmallWater.Playing = true
				resources.OceanWater.Volume = 0
				resources.SmallWater.Volume = 0
				local Generators = WaterF:FindFirstChild("WaterGenerators")
				if not Generators then
					warn("Couldn't get water generators folder!")
				else
					for _, z in pairs(Generators:GetDescendants()) do
						if not z then warn("Couldn't find anything in" .. Generators.Name .. "!") return end
						if z:IsA("BasePart") then -- checks if it's a part
							if z:FindFirstChild("Include_Sound?") and z["Include_Sound?"].Value == true then -- just checks if the part will include the sound effect or not, if it's set to false it'll just not add the sound effect.
								if z.Name == "Ocean_Water" and z:IsA("BasePart") then
									local ows = resources.OceanWater:Clone() -- applies Ocean water sound to the Ocean water part
									ows.Parent = z
									ows.Volume = 1
									ows.TimePosition = math.random(0, ows.TimeLength)
									ows.RollOffMaxDistance = z and math.min((math.sqrt(z.Size.X^2 + z.Size.Z^2) / 2) * WaterRollOffMultiplier, 1000) or 500





								elseif z.Name == "Small_Water" and z:IsA("BasePart") then
									local sws = resources.SmallWater:Clone() -- applies small water sound to the small water part
									sws.Parent = z
									sws.Volume = 1
									sws.TimePosition = math.random(0, sws.TimeLength)
								end

							end
							task.wait() -- wait time for checking all water parts #Sigma

							local success, err = pcall(function()
								z.Parent = workspace.Filter.WaterCurrents
								local WaterSourceForce = Instance.new("NumberValue")
								WaterSourceForce.Name = "Force"
								WaterSourceForce.Value = 0
								WaterSourceForce.Parent = z
								game.Workspace.Terrain:FillBlock(z.CFrame, z.Size, Enum.Material.Water) -- fills the parts found with water.
							end)

							if success then
								z.Transparency = 1
								z.CastShadow = false
								z.CanCollide = false
								z.CanTouch = true
							else
								z.CanCollide = true
								z.Transparency = 0.5
								z.Color = Color3.fromRGB(255, 0, 0)
								warn("Failed to fill block with water for part " .. z.Name .. ": " .. err)
								warn("Attempting to resize part until it succeeds")
								local Decrease_Increment = 1
								repeat 
									wait()
									warn("Resize part attempt")
									z.Size = z.Size - Vector3.new(z.Size.X, Decrease_Increment, z.Size.Z)
									success, err = pcall(function()
										game.Workspace.Terrain:FillBlock(z.CFrame, z.Size, Enum.Material.Water)
									end)
									z.Transparency = z.Transparency - Decrease_Increment

									Decrease_Increment = Decrease_Increment + 0.05
								until success

								if success then
									z.Transparency = 1
									z.CastShadow = false
									z.CanCollide = false
									z.CanTouch = true
								else
									warn("Failed to fill block with water for part " .. z.Name .. " after resizing: " .. err)
								end
							end


						end
					end
				end
			end
		end
		local x = Instance.new("BoolValue")

		x.Name = "WaterLoaded"

		x.Value = true
		x.Parent = ReplicatedStorage
		warn("-- Baseplate World Water Loaded! --")


	end
	local SkipWaterInStudio = true
	if not is_studio or not SkipWaterInStudio  then
	SetupWater()
	elseif SkipWaterInStudio then
		if not ReplicatedStorage:FindFirstChild("WaterLoaded") then
		local x = Instance.new("BoolValue")

		x.Name = "WaterLoaded"

		x.Value = true
		x.Parent = ReplicatedStorage
		warn("-- Baseplate World Water Loaded! --")
		end
	end
	local x = Instance.new("BoolValue") -- THE BACK-BONE OF BaseplateWorld!!!

	x.Name = "GameLoaded"

	x.Value = true
	x.Parent = ReplicatedStorage

	print("server loaded")