Better Way To Reference an Object

Hello guys, is there a better way to reference an object in my situation.

My current situation is that I have a Train. The Train gets destroyed and regenerated. So, for an extremely little amount of time, the train is gone from the game.

However, this means I have to keep referencing train every time I want to use it, so as to ensure I am referencing the current Train and not the destroyed one.

Does anyone have a better method than me just referencing?


Wrote this script here so the format might be incorrect

while true do
local Train = game.workspace:waitforchild("Train")
--Train Stuff and Code

Using OOP, you can create a train object that will continue to exist even after the physical model itself is destroyed (for example you can add the physical model of the train as an attribute of the train object).

1 Like

The better way should be to use a signal or bindable event and fire this signal containing a reference to the object everytime the object had regenerated.

I think you could also do

local Train

        if Child.Name == "Train") then
        Train = Child

let me know if this works (i have never tried this before)