Help with water script

Im trying to make it so that when the player’s head touches water, they start losing health and a blurry effect appears on their screen and it works but whenever the player exits the water the effect stays

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild('Humanoid')
local health = humanoid.Health

local function enableFX(inWater)
	if inWater == true then
		game:GetService('Lighting').WATER_BLUR.Enabled = true
		game:GetService('Lighting').WATER_COLOR.Enabled = true
	end
	if inWater == false then
		game:GetService('Lighting').WATER_BLUR.Enabled = false
		game:GetService('Lighting').WATER_COLOR.Enabled = false
	end
end

while wait() do
	local head = character:FindFirstChild("Head")
	local min = head.Position - (4 * head.Size)
	local max = head.Position + (4 * head.Size)	
	local region = Region3.new(min,max):ExpandToGrid(4)
	local material = workspace.Terrain:ReadVoxels(region,4)[1][1][1]
	if material == Enum.Material.Water then
		enableFX(true)
		wait(20)
		repeat
			wait(1)
			humanoid.Health = humanoid.Health -5
		until
		humanoid.Health == 0 or material ~= Enum.Material.Water
	else
		enableFX(false)
	end
end

1 Like

Try reducing the min & max values, I believe that 4 * head.Size is too much.

	local min = head.Position - (0.5 * head.Size)
	local max = head.Position + (0.5 * head.Size)	

Alternatively, you can look at this demo by @Prototrode. Output prints “true” when swimming and “false” when player exits the water.
swimming.rbxl (43.1 KB)