This method did not work however you gave me an idea, what I did was to place a

wait(1);

at the beginning of the function and now it works great!

Thanks if it were not for your help I would have never arrived at this solution.