My script to change water transparency not working?

I have a script all based around making water realistic, playing with the sounds and now meant to be changing the water transparency, everything about the script works fine, except for one part which is meant to change the transparency of the water when you move the camera in and out of it, this camera in the water system works for the sound so I know that isn’t the problem. The script outputs no errors, but no change to the water transparency, my graphics is turned up so that isn’t the problem, the properties tab doesn’t change when the script is in action, I feel like this should work just fine so either I’m thinking that you can’t locally change the water transparency for a player or that somehow it just isn’t working the way it is meant to. Could someone tell me what is happening?

cam = workspace.CurrentCamera
char = script.Parent
underwater = game.SoundService.Sounds.Underwater
swimming = game.SoundService.Sounds.Swimming
terrain = workspace.Terrain

local humanoid = char:WaitForChild("Humanoid")
local Terrain = game.Workspace.Terrain
local Transparency = Terrain.WaterTransparency

offset = 0.01

while wait() do
	local pos = cam.CFrame.Position

	local min = Vector3.new(pos.X + offset,pos.Y + offset, pos.Z + offset)
	local max = Vector3.new(pos.X - offset,pos.Y - offset, pos.Z - offset)

	local region = Region3.new(max, min)
	region = region:ExpandToGrid(4)

	if region then
		local material = game.Workspace.Terrain:ReadVoxels(region, 4)
		if material[1][1][1] == Enum.Material.Water then
			underwater.Playing = true
			swimming.Playing = false
			Transparency = 0.02
		else
			underwater.Playing = false
			Transparency = 0.45
			if humanoid:GetState() == Enum.HumanoidStateType.Swimming then	
				swimming.Playing = true

            end
		end
		end
			
end
2 Likes

I believe that this should work:


cam = workspace.CurrentCamera
char = script.Parent
underwater = game.SoundService.Sounds.Underwater
swimming = game.SoundService.Sounds.Swimming
terrain = workspace.Terrain

local humanoid = char:WaitForChild("Humanoid")
local Terrain = game.Workspace.Terrain

offset = 0.01

while wait() do
	local pos = cam.CFrame.Position

	local min = Vector3.new(pos.X + offset,pos.Y + offset, pos.Z + offset)
	local max = Vector3.new(pos.X - offset,pos.Y - offset, pos.Z - offset)

	local region = Region3.new(max, min)
	region = region:ExpandToGrid(4)

	if region then
		local material = game.Workspace.Terrain:ReadVoxels(region, 4)
		if material[1][1][1] == Enum.Material.Water then
			underwater.Playing = true
			swimming.Playing = false
			Terrain.WaterTransparency= 0.02
		else
			underwater.Playing = false
			Terrain.WaterTransparency= 0.45
			if humanoid:GetState() == Enum.HumanoidStateType.Swimming then	
				swimming.Playing = true

            end
		end
		end
			
end

I think you may be calling the VALUE of the water at the start of the script, instead of the property. To fix this, I removed the Transparency variable and just used Terrain.WaterTransparency instead

Hope this helps!

This is perfect, thankyou so much