So basically i asked a few days ago for some help with a function i needed to make and a user gave me this function here that seems to solve exactly what i needed to do: