Hey, I’m trying to make an underwater effect. This effect is enabled when a player puts their character into the water part. This effect sadly only works for one part and then doesn’t work for the rest.
This uses Collection Service Tags so I can clone the part and put it in any folder/place. This script is located inside StarterPlayerScripts.
local Lighting = game:GetService("Lighting")
local camera = workspace:WaitForChild("Camera")
local collectionService = game:GetService("CollectionService")
local waterParts = collectionService:GetTagged("water_parts")
local waterBlur = Lighting:WaitForChild("waterBlur")
local waterColor = Lighting:WaitForChild("waterColor")
local waterDOF = Lighting:WaitForChild("waterDOF")
for _, water in pairs (waterParts) do
RunService.Stepped:Connect(function()
local CameraV3 = water.CFrame:PointToObjectSpace(camera.CFrame.Position)
local CameraUnderwater = (math.abs(CameraV3.X) <= water.Size.X / 2)
and (math.abs(CameraV3.Y) <= water.Size.Y / 2)
and (math.abs(CameraV3.Z) <= water.Size.Z / 2)
waterBlur.Enabled = CameraUnderwater
waterColor.Enabled = CameraUnderwater
waterDOF.Enabled = CameraUnderwater
end)
end```
your loops seem to be in the wrong order. for every water part you had, you were overriding the stepped connection to only check specifically for that part. instead you should be looping over every part on every step
local Lighting = game:GetService("Lighting")
local camera = workspace:WaitForChild("Camera")
local collectionService = game:GetService("CollectionService")
local waterParts = collectionService:GetTagged("water_parts")
local waterBlur = Lighting:WaitForChild("waterBlur")
local waterColor = Lighting:WaitForChild("waterColor")
local waterDOF = Lighting:WaitForChild("waterDOF")
RunService.Stepped:Connect(function() --i would recommend you use RenderStepped here instead
local CameraUnderwater = false
for _, water in pairs (waterParts) do --move the loop inside of the stepped connection instead
local CameraV3 = water.CFrame:PointToObjectSpace(camera.CFrame.Position)
CameraUnderwater = (math.abs(CameraV3.X) <= water.Size.X / 2)
and (math.abs(CameraV3.Y) <= water.Size.Y / 2)
and (math.abs(CameraV3.Z) <= water.Size.Z / 2)
if CameraUnderwater then --if the camera is underwater, we don't need to bother checking for water anymore
break
end
end
waterBlur.Enabled = CameraUnderwater --moved out of the loop, so it doesn't reset for every water part that isn't detected
waterColor.Enabled = CameraUnderwater
waterDOF.Enabled = CameraUnderwater
end)