There must be an earthquake detector in place to detect any earthquakes off the shores in the oceans. This system can be a regional or local system, and although the system is fallible the margin of error is very small.
Sometimes though the earth quake/movement is too close to shore to detect it in time, and people get caught in it as a result.
There also needs to be a universal warning signal that actually tells people that the tsunami is coming.
I hope this helped.