I want to save a door’s initial position into a variable, but whenever it gets called by a line of code it references the door’s changed position, which is the expected behavior (it’s position changes, fyi) and I clearly don’t want that. I want to store it’s initial value before it was moved by the script later on, but don’t really know a way of doing that instead of maybe saving it to a Vector3 Value (which is an Object, however, and I want to keep the “door system” script based).
local originalPosition= {} --table where the position will be stored in
if not table.find(originalPosition, doorPosition) then
originalPosition[doorPosition] = doorName.Position -- adds the original position of the door (change the door name if different name)
end
local mainPosition = originalPosition[doorPosition] --variable that calls out the original position of the door (use that variable as the original position)
A good suggestion however that code is pretty bulky and I think there is a function for doing what I specified in the post, however I can’t just remember what it was…
To me, it sounds simple really. Just get the door’s initial position as soon as the game starts and store it in a variable. Thats it, you dont have to modify it anymore. If this doesnt work could I get a sample of a chunk of code that is responsible for finding the door’s initial position?
Heres an example of how it could be done:
local initialPosition = game.Workspace:WaitForChild("DoorName").DoorPart.Position
Also, in response to your statement about an apparent function, I dont really know if one exists, but anyone can correct me if im wrong. Also, adding vector3.new is not neccesary when using the variable mentioned above to change the door position, I have tested it in my own game.
Well you see the problem with that is that it’s going to get the door’s current position as I said in the original post.
In my understanding, it would work something like this.
Also, that :WaitForChild you added doesn’t change anything. It’s still the same part*, right? It’s just waiting for it to be added to the workspace and then spews out the value. Correct me if I’m wrong.
local foobar = script.Parent.Position -- The door's position, let's say it begins at 0,0,0
print(foobar) -- Prints the position 0,0,0
script.Parent.Position = Vector3.new(5,0,0)
print(foobar) -- Prints the position, however now it's 5,0,0 and not 0,0,0
…which sounds logical to me
P.S. I think I found out a way to store it, however if you could give us a solution then other people could make use of it.
That’s false. It’s good practice to test the code yourself before making final assumptions about its behavior.
local foobar = script.Parent.Position -- The door's position, let's say it begins at 0,0,0
print(foobar) -- Prints the position 0,0,0
script.Parent.Position = Vector3.new(5,0,0)
print(foobar) -- Prints the position 0,0,0
The only case a variable would reference the current state of a variable is with tables and instances, as the variable is a reference to their place in memory rather than a value itself.
It actually works! Although the part has to be anchored or else it would save the position like .1 milliseconds after it starts falling thus displaying an inaccurate value but still, it works!