I think it will help you understand if you know the historical context. The concept of valency existed well before chemical bond, electrons, protons, etc. were known. It is a primitive concept from late 1860s. It referred to the capacity of an element to bind with hydrogen or any other element.
For example, if we say carbon has a valency of four, all it means that it can combine with 4 hydrogen atoms. If we have say oxygen has a valency of 2, it means it will combine with two hydrogen, the famous H$_2$O! Does this make it clear? One would determine valency by pure elemental analysis. If I have FeCl$_3$, iron has a valency (=combining power) of 3, because it can combine with three chlorine atoms. I am avoiding the word ion, because ions did not exist then! The valency of oxygen in peroxide is still 2 because each oxygen atom is combining with two atoms via single bonds. In this case, the other atom is another oxygen itself.
Now read the Wikipedia article Valence. Note that I did not assign any sign to valency
Oxidation number or state is a newer notion which came about when electrons were very well recognized to play a role in bonding and the atomic structure become relatively well understood. I quote it's definition from the OED
a number representing the degree of oxidation of an element in a
particular ion, molecule, or substance, being a notional electric
charge equal to the number of electrons lost (or gained, if the number
is negative) by an atom of the element in forming the species or
substance, all bonding being assumed to be completely ionic
Oxidation state/ number has a sign associated with it. Sometimes, valency and oxidation number can match in terms of their absolute values.
Valency of nitrogen= 3
Oxidation state of nitrogen = -3