How to structure your code, scripts and project

I want to know how to write code that is faster, more flexible, more readable and more maintainable

here is an example of my code:

-- Services
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")

-- Variables
local debounces = {}
local cooldown = 1
local damage = 10

-- Run
Players.PlayerAdded:Connect(function(player)
	debounces[player] = false
end)

Players.PlayerRemoving:Connect(function(player)
	debounces[player] = nil
end)

for _, Water in ipairs(CollectionService:GetTagged("Water")) do
	Water.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid == nil then
			return
		end
		
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		if player == nil then
			return
		end
		
		if debounces[player] == true then
			return
		end
		
		humanoid:TakeDamage(damage)
		
		debounces[player] = true
		
		task.wait(cooldown)
		debounces[player] = false
		
		while true do
			if hit == nil or hit.Parent == nil then
				return
			end
			
			local isTouchingCharacter = false
			for _, basePart in pairs(hit.Parent:GetDescendants()) do
				if basePart:IsA("BasePart") == false then
					return
				end
				
				local touchInterest = basePart.Touched:Connect(function()
					
				end)
				
				if basePart:GetTouchingParts()[Water] ~= nil then
					isTouchingCharacter = true
				end
				
				touchInterest:Disconnect()
			end
			if isTouchingCharacter == false then
				break
			end
			
			if player == nil or debounces[player] == true then
				break
			end
			
			if humanoid == nil  then
				break
			end
			
			humanoid:TakeDamage(damage)
			
			task.wait(cooldown)
			debounces[player] = false
		end
	end)
end

thanks for your time, knowledge and support