How do I optimize this script?

Some mistakes upon first reading the code,

You shouldn’t connect this event in the for loop, this will repeatedly connect the same event even though it only needs to be connected once. Move it to the bottom of the script.

By the looks of it (assuming this is voxels) you aren’t implementing any form of greedy meshing, which means that you are rendering triangles for every block instead of greedy meshing it.

@/5uphi 's video explains greedy meshing pretty well

as well as this write-up by Elttob Consume everything - how greedy meshing works

1 Like