The general rule is that if a beneficiary dies before the testator (the will maker), the gift or legacy does not take place (and so it goes back into the residue of the estate).
The exceptions are:
if the will contains an alternative or substitute name
if the gift is made to two or more people as joint tenants (the gift goes to the survivor)
if the gift or legacy is to a child or other descendant of the testator, and that descendant dies before the testator, the gift or legacy passes to any children of the deceased as a share
The above is true in England & Wales - no idea if Scotland is any different