Search code examples
luaroblox

underwater camera effects on roblox


i need help debugging this code, i am trying to make an effect where the screen is tinted a blueish color and the reverb is set to bathroom whenever a camera is inside a part named "water", it works for 1 body of water in the game and the rest will not cause the effect to happen, despite it printing for every body of water in the game, im assuming the one it picks to work is the one that is loaded first, but im not sure why only that one registers, it checks for them all but only that one part actually causes it to happen

while true do
    wait(0.1)
    for i,v in pairs(game.Workspace:GetChildren()) do
        if v.Name == "water" then
            print ("lolhehehhevljl")
            local parms = RaycastParams.new()
            parms.FilterDescendantsInstances = {v}
            parms.FilterType = Enum.RaycastFilterType.Whitelist
            local ray = require(game.Workspace.Modules.raymodule).raycast(game.Workspace.CurrentCamera.CFrame.Position, v.Position, parms)
            if ray then
                game.Lighting.ColorCorrection.TintColor = Color3.new(1, 1, 1)
                game.SoundService.AmbientReverb = Enum.ReverbType.NoReverb
            else
                game.Lighting.ColorCorrection.TintColor = Color3.new(0, 0.65098, 1)
                game.SoundService.AmbientReverb = Enum.ReverbType.Bathroom
            end
        end
    end
end

Solution

  • It could be that your logic is fighting with itself. You are checking that you are underwater for each block, and if one says yes, but the next says no, you will essentially be undoing any changes that should be happening.

    So one fix might be to ask all of the water blocks, "am I underwater?" And if any say yes, then change the lighting and reverb, but if ALL of them say no, then change it back.

    -- local variables
    local Modules = game.Workspace.Modules
    local raymodule = require(Modules.raymodule)
    local TINT_UNDERWATER = Color3.new(0, 0.65098, 1)
    local TINT_NORMAL = Color3.new(1, 1, 1)
    
    -- find all of the water parts
    local water = {}
    for i, v in pairs(game.Workspace:GetChildren()) do
        if v.Name == "water" then
            table.insert(water, v)
        end
    end
    
    -- create a filtered list for raycasting
    local raycastParams = RaycastParams.new()
    raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
    raycastParams.FilterDescendantsInstances = water
    
    -- every frame, check if we are underwater
    game.RunService.Heartbeat:Connect(function()
        local isUnderwater = false
        local cameraPos = game.Workspace.CurrentCamera.CFrame.Position
    
        for i, v in ipairs(water) do
            local didHit = raymodule.raycast(cameraPos, v.Position, raycastParams)
            if not didHit then
                isUnderwater = true
            end
        end
    
        -- update the camera state
        if isUnderwater then
            game.Lighting.ColorCorrection.TintColor = TINT_UNDERWATER
            game.SoundService.AmbientReverb = Enum.ReverbType.Bathroom
        else
            game.Lighting.ColorCorrection.TintColor = TINT_NORMAL
            game.SoundService.AmbientReverb = Enum.ReverbType.NoReverb
         end
    end)