As you’ve probably read, I pretty much almost murdered studio by trying to create an underwater script, and am hoping if anybody is able to point me in the right direction.
code that almost crashed studio:
workspace.CurrentCamera.Changed:Connect(function()
wait()
local castPoints = {game.Workspace._Water.Position}
local ignoreList = {}
local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints,ignoreList)
for _, c in pairs(parts:GetChildren()) do
wait(1)
if c.Name == "_Water" then
workspace.CurrentCamera.Underwater_Amb.Enabled = true
else
workspace.CurrentCamera.Underwater_Amb.Enabled = false
end
end
end)
As you can see I used the Changed event (which is a silly mistake to rely on,) but I’m trying to obtain the effect of when somebody’s camera is in the water, and then the ColorCorrectionEffect enables. However, it’s not working really well.
The loop isn’t the problem I’m having. I’m having a problem with creating a an underwater ambient script where if the CurrentCamera is obscured by a Part, the ColorCorrectionEffect will enable true.
It’s better to use RenderStepped or a RunService event. Camera easily changes just by moving one stud so it probably doesn’t care about CFrame changes that much anyways.
Is anything appearing in the Output? This doesn’t look like it would work. The camera that appears in Workspace in studio is not the camera the player will use. Try creating the Underwater_Amb instance at the top of your script and parent it to the camera that way. I’d assume constant errors would cause lag.
Also;
Just do parts. :GetPartsObscuringTarget() returns an array, not an instance.
As @Xiousa said, the Underwater_Amb instance is not a child of the player’s camera, so I would place it under Lighting instead. Applying the removal of :GetChildren() and a bool value for readability I ended up with this working version of your script. I hope this is helpful!
workspace.CurrentCamera.Changed:Connect(function()
local castPoints = {game.Workspace._Water.Position}
local ignoreList = {}
local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints,ignoreList)
local isWater = false
-- Check for water parts [if we found any parts]
if #parts > 0 then
for _, c in pairs(parts) do
if c.Name == "_Water" then
isWater = true
break
end
end
end
if isWater then
game.Lighting.Underwater_Amb.Enabled = false
else
game.Lighting.Underwater_Amb.Enabled = true
end
end)
Your solution almost crashed studio, so I tweaked it a little and now it works like a charm!
while true do
wait(0.025)
local castPoints = {game.Workspace._Water.Position}
local ignoreList = {}
local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints,ignoreList)
local isWater = false
-- Check for water parts [if we found any parts]
if #parts > 0 then
for _, c in pairs(parts) do
if c.Name == "_Water" then
isWater = true
break
end
end
end
if isWater then
game.Lighting.Underwater_Amb.Enabled = false
else
game.Lighting.Underwater_Amb.Enabled = true
end
end
Edit: I might tweak it more later just because while true do loops seem to be inefficient. Anyhow, thanks!