Swim script not working

The swim script doesn’t work, even though it should?

(It is a local script that sits in StarterPlayerCharacters)

			game:GetService("RunService").Heartbeat:Connect(function()
				local swimrn = true
				print("Beginning swim test")
				if val.Value == true then
					db.Value = config.touchendticks
					local swimrn = true
					print("Can swim. Reset DB.")
				else
					db.Value -= 1
					print("Removed one DB. ".. tostring(db.Value))
				end
				if db.Value < 1 then
					swimrn = false
				end
				print("Swimming begin if is true: ".. tostring(swimrn))
				if swimrn == true then
					print("Swimming begun")

					Humanoid:SetStateEnabled("GettingUp", false)
					if currentswim == false or Instance.new("Humanoid"):GetState() ~= Enum.HumanoidStateType.Swimming then
						Humanoid:ChangeState("Swimming")
					end
					currentswim = true
					breathh.Value -= 1
					if breathh.Value < 1 then
						Humanoid.Health = 0
					end
				else
					print("Swimming ended")
					currentswim = false
					Humanoid:SetStateEnabled("GettingUp", true)
					breathh.Value = config.breath
				end
				print("Ending swim test")
			end)

Edit: There are no erros and the script doesn’t yield, it’s a issue with the character going in and out of swimming state

Make a server-script inside startercharacter.

local Character = script.Parent
local NotSwimming = false
local Humanoid: Humanoid? = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid", 25) -- incase player lags during spawn. 
Humanoid.Swimming:Connect(function() -- player started swimming
 if not NotSwimming then
		NotSwimming = true
		repeat 
			task.wait(1)
			breathh.Value -= 1
		until breathh.Value == 0
		while NotSwimming do
			task.wait(1)
			Humanoid:TakeDamage(15)
			if breathh.Value > 0 then
				break
			end
			if not NotSwimming then
				break
			end
			if Humanoid.Health <= 0 then
				break
			end
		end
	end
end)
Humanoid.StateChanged:Connect(function(Old,New)
  if Old == Enum.HumanoidStateType.Swimming then -- no longer swimming
       NotSwimming = false
       breathh.Value = config.breath
   end
end)

Using a looping function is useless as there are already connections meant for detecting swimming and humanoid states.

The script is to swim in a BasePart named water, and also some extra info: making it a server script didn’t work. And the issue occurs because jumping would change the state but it would automatically change it back.