How to completely replace an instance and all it's indexed variables with another instance?

What I’m trying to do is basically reassign an instance’s id or however it’s kept track of in the world so that all the variables that I already have for it are for the instance I replaced it with.

For example, I have a part in the workspace and it is indexed as local part = game.Workspace:WaitForChild(“Part”). I want to “reassign” the part’s instance number or whatever it is to an existing part named Part2 so that the part variable will then be referencing Part2 instead of Part.

The issue here is that I have no idea how roblox works beyond the lua so I don’t even know if this is possible at all much less possible with in-game scripts. Does anyone have any ideas?

When assigning a variable to an Instance, it stores a reference to that, so there’s no way to reassign all of them.

This really isn’t something I can think of a valid use case for. What are you trying to accomplish?

You could instead store an Instance as a value in a table, and reassign that when you want to do this.

It is technically possible to automate this with environment manipulation, but that’s an awful solution.

Unless I am mistaken, all you have to do is assign part to the object you want the reference to be assigned to like this:

part = workspace.Part2

That is all. The original part will be completely de-referenced unless you create another variable for it.

However, make sure you do not use local before the variable part as this will create another reference, and your code will be confused on which part you will be referring to.

Thanks but I’m trying to reassign every variable for the given part in all scripts.

I understand now.

There is this variable _G and modules, however, I use modules rather than _G.
This is a thread from awhile back that has a better way of explaining it than I do at the moment. Currently in class. I know, shame on me.

I guess you could use object values but thats deprecated

Where did you get this from? ObjectValues are still perfectly fine to use.
Check the api if you wish.

They are still fine to use, but values are usually easily avoidable and can result in making more complex systems to work with. I usually only ever use them for something simple like leaderstats, permanent references/links or a server message, but nothing really complicated. I prefer to use objects, classes, and tables instead when applicable.