I was trying to modify a water script to work for multiple parts. However, my attempt was not very useful and I could use some guidance on how to fix this to work for my case.
Here is the original script:
local camera = workspace.CurrentCamera
local waterPart = workspace:WaitForChild("water")
local rs = game:GetService("RunService")
local blur = Instance.new("BlurEffect")
local colorCorrection = Instance.new("ColorCorrectionEffect")
blur.Enabled = false
colorCorrection.Enabled = false
colorCorrection.TintColor = Color3.fromRGB(108,135,255)
blur.Parent = game.Lighting
colorCorrection.Parent = game.Lighting
rs:BindToRenderStep("underwaterCamera", 201, function()
local objectSpaceCameraPosition = waterPart.CFrame:PointToObjectSpace(camera.CFrame.Position)
local waterDepth = math.abs(objectSpaceCameraPosition.Y - waterPart.Size.Y/2) -- How far the player is below the water level
local inWater = true;
if (math.abs(objectSpaceCameraPosition.X) > waterPart.Size.X/2) then
-- Out of bounds on the X axis
inWater = false;
end
if (math.abs(objectSpaceCameraPosition.Y) > waterPart.Size.Y/2) then
-- Out of bounds on the Y axis
inWater = false;
end
if (math.abs(objectSpaceCameraPosition.Z) > waterPart.Size.Z/2) then
-- Out of bounds on the Z axis
inWater = false;
end
blur.Enabled = inWater;
colorCorrection.Enabled = inWater;
end)
Here is the one where I tried to modify it:
local waterPart = workspace.Folder:WaitForChild("water")
local rs = game:GetService("RunService")
local blur = Instance.new("BlurEffect")
local colorCorrection = Instance.new("ColorCorrectionEffect")
blur.Enabled = false
colorCorrection.Enabled = false
colorCorrection.TintColor = Color3.fromRGB(108,135,255)
blur.Parent = game.Lighting
colorCorrection.Parent = game.Lighting
for i,v in ipairs(workspace.Folder:GetChildren()) do
rs:BindToRenderStep("underwaterCamera", 201, function()
local objectSpaceCameraPosition = waterPart.CFrame:PointToObjectSpace(camera.CFrame.Position)
local waterDepth = math.abs(objectSpaceCameraPosition.Y - waterPart.Size.Y/2) -- How far the player is below the water level
local inWater = true;
if (math.abs(objectSpaceCameraPosition.X) > waterPart.Size.X/2) then
-- Out of bounds on the X axis
inWater = false;
end
if (math.abs(objectSpaceCameraPosition.Y) > waterPart.Size.Y/2) then
-- Out of bounds on the Y axis
inWater = false;
end
if (math.abs(objectSpaceCameraPosition.Z) > waterPart.Size.Z/2) then
-- Out of bounds on the Z axis
inWater = false;
end
blur.Enabled = inWater;
colorCorrection.Enabled = inWater;
end)
end
Hi, kinda your post needs more info and context, whats the problem?
That script looks like its changing the Visual FX of lighting, when a player is getting down into the water, on a render step its changing those values of “lightin”
Then you added a loop. For Each thing inside a folder in ur workspace you are binding the same thing that was before… What you are doing in there? Whats inside that folder? and I guess you are not even using that “v” for each children in the folder.
Since your “water” is a bunch of parts which makes it alot easier to deal with than the actual water terrain (from what I’ve seen in the script).
I tried using Region3 but it was kinda inaccurate and hard to deal with so I tried using a bunch of if and comparision and it seems to do the trick.
-- Local script
local WaterFolder = game.Workspace.Water
local Blur = game.Lighting.Blur
local ColorCorrection = game.Lighting.ColorCorrection
local Camera = workspace.CurrentCamera
game:GetService("RunService").Heartbeat:Connect(function() --RenderStepped is also fine.
local Position = Camera.CFrame.Position
for _,Part in pairs(WaterFolder:GetChildren()) do
if Part:IsA("BasePart")
and Position.X > Part.Position.X - Part.Size.X/2
and Position.X < Part.Position.X + Part.Size.X/2
and Position.Y > Part.Position.Y - Part.Size.Y/2
and Position.Y < Part.Position.Y + Part.Size.Y/2
and Position.Z > Part.Position.Z - Part.Size.Z/2
and Position.Z < Part.Position.Z + Part.Size.Z/2
then
-- Alot of comparison for maximum accuracy!
-- Player's camera is in the water
Blur.Enabled = true
ColorCorrection.Enabled = true
break -- Break the loop so that it doesn't check again if Player's camera is in or isn't in the water
else
-- Player's camera is not in the water
Blur.Enabled = false
ColorCorrection.Enabled = false
end
end
end)
local DetectTerrainWater = true
local TouchPart = Instance.new("Part", workspace)
TouchPart.Anchored = true
TouchPart.CanCollide = false
TouchPart.Size = Vector3.new(1, 1, 1)
TouchPart.Transparency = 1
TouchPart.Name = "TouchPart"
local Blur = Instance.new("BlurEffect", game.Lighting)
Blur.Enabled = false
local ColorCorrection = Instance.new("ColorCorrectionEffect", game.Lighting)
ColorCorrection.Enabled = false
ColorCorrection.TintColor = Color3.fromRGB(11, 143, 213)
ColorCorrection.Contrast = 0.5
ColorCorrection.Brightness = 0.4
ColorCorrection.Saturation = 0.6
local UnderwaterAmbienceSound = Instance.new("Sound", workspace.CurrentCamera)
UnderwaterAmbienceSound.Name = "UnderwaterAmbienceSound"
UnderwaterAmbienceSound.SoundId = "rbxassetid://4626145950"
if not UnderwaterAmbienceSound.IsLoaded then
UnderwaterAmbienceSound.Loaded:Wait()
end
UnderwaterAmbienceSound.Volume = 0
UnderwaterAmbienceSound.Looped = true
UnderwaterAmbienceSound:Play()
local TouchConnection = TouchPart.Touched:Connect(function() end)
--TouchConnection:Disconnect()
workspace.CurrentCamera:GetPropertyChangedSignal("CFrame"):Connect(function()
TouchPart.CFrame = workspace.CurrentCamera.CFrame
local WaterFound = false
if DetectTerrainWater then
local min = TouchPart.Position - (4 * TouchPart.Size)
local max = TouchPart.Position + (4 * TouchPart.Size)
local region = Region3.new(min, max)
local materials, occupancies = workspace.Terrain:ReadVoxels(region, 4)
local size = materials.Size
for x = 1, size.X, 1 do
for y = 1, size.Y, 1 do
for z = 1, size.Z, 1 do
if materials[x][y][z].Name == "Water" then
WaterFound = true
end
end
end
end
end
if not WaterFound then
local TouchingParts = TouchPart:GetTouchingParts()
for _, Part in pairs(TouchingParts) do
if Part.Name:lower():find("Water") or Part.Parent.Name:lower():find("Water") then
WaterFound = true
break
end
end
end
if WaterFound then
UnderwaterAmbienceSound.Volume = 0.5
ColorCorrection.Enabled = true
Blur.Enabled = true
else
UnderwaterAmbienceSound.Volume = 0
ColorCorrection.Enabled = false
Blur.Enabled = false
end
end)