One question, if I send a table of players to a function or module does the module or function get a copy

Could this mean in reality my players wont move. I was thinking this because changing variables in functions don’t work and in c functions get copies of the value, so will the function or module script get a copy of the player. Would this cause weird errors? Really I am confused. Thank you for

Objects and tables are passed by reference, no copies are made unless in specific cases. An example of a copy would be you sending a table over a remote or bindable object. However just passing them around as arguments to any function shouldn’t cause a copy.

Then why does this create a copy

x = 0

function add(num)

num = num + 434




This still prints 0 so I think a copy was created, uh am I missing something here, wait so only objects and tables are excluded from this but number values arent?

Numbers are passed by value rather than passed by reference, so yes, the num parameter is a copy of the argument passed in. Changes made inside the function will not affect the original variable.

So is there a reason whty people tell you not to pass in parts or tables even trhough they aren’t copied. Is it bad or something

When sending a table to a function, a form of a copy is made which is later on discarded once the function has been completed. Same logic for modules.

Its important to know what your passing to what function, like is it an object (Player) is it a string (Player.Name) or is it a property (Player.Character).

What I am passing in is an object shouldnt the changes work since the other guy said values passed in by references change like parts

Depends on how you are using what you passed and if you passed it correctly.

If I pass in game.workspace.Baseplate into a function it would change but if I passed in a variable with baseplate it wouldnt change right? Or what would be the case of passing in uit incorrectly

If you pass a part into a function, assigning any of its properties will change the base part. Copies are not made, you’re passing a reference to it. You can assign the reference variable, which is different, but assigning into the value the reference holds changes it.

part.Property = Value -- changes the actual part property
part = someOtherPart -- re-binds `part` variable without affecting its original value

This is the behavior for tables too.

So is there a case where a copy would be made

There isn’t any case where a copy would be made. As Autterfly said, you’re just passing a reference to the value.

Ok so numbers and strings are the only ones which get copied, all of this is the same for modules, right?

Per Lua 5.1 Reference Manual

“Tables, functions, threads, and (full) userdata values are objects : variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.”

And the rest, " nil , boolean , number , string" are (effectively) passed by value.