Using TweenService with a model / part cloned from ReplicatedStorage

I am trying to make an elevator that has sliding doors I am storing the elevator model in ReplicatedStorage and cloning the model to a generated map. (this is done in another script)

The elevator doors / button are children of the elevator model.
image

The elevator model retains its origin position when it is in ReplicatedStorage.
image

The problem that I am having is when I try to animate the elevator doors. SInce the elevator is cloned and then moved to a random location on my generated map, I need the doors to move relative to their current position.

When I use TweenService to animate the doors, they don’t move +4 or -4 studs as expected. Instead, they fly off into the distance towards the Origin Position that the model was originally located at.

This code works as expected when the model is not cloned from ReplicatedStorage and moved to a new location, and the elevator doors animate normally, i.e. when the model is normally placed in the Workspace. What is going on here?

Here is the code that I am using to animate the doors:

local tweenService = game:GetService("TweenService")

-- Objects associated with the elevator
local clickDetector = script.Parent.ClickDetector
local elevator_left_door = script.Parent.Parent.elevator_door1
local elevator_right_door = script.Parent.Parent.elevator_door2

-- Details about how the elevator door animation will be played
local tweenInfo = TweenInfo.new(
	5, --Duration in seconds
	Enum.EasingStyle.Linear, --EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	1 -- DelayTime in seconds
)

-- End point for the door movement
local tween_goal_left = {Position = elevator_left_door.Position + Vector3.new(-4,0,0)}
local tween_goal_right = {Position = elevator_right_door.Position + Vector3.new(4,0,0)}

-- Creates an object that can move the elevator doors
local tween_left = tweenService:Create(elevator_left_door,tweenInfo,tween_goal_left)
local tween_right = tweenService:Create(elevator_right_door,tweenInfo,tween_goal_right)

-- Open the elevator doors when the elevator button is clicked by the player
clickDetector.MouseClick:Connect(function()
	tween_left:Play()
	tween_right:Play()
end)

Is there a way I can get the current position of the elevator doors? It seems like the Position property that I am using is getting the original position of the doors, and not their position after the Model is cloned to a new location.

Also, is there a better place to store models than ReplicatedStorage for this use case?

Model:GetPivot().Position can be used to retrieve a model’s current position.
https://developer.roblox.com/en-us/api-reference/function/PVInstance/GetPivot

2 Likes

This worked, I also had to move some of the code into the MouseClick function so that the variables would be created upon mouse click rather than the server start.

Updated code:

local tweenService = game:GetService("TweenService")

-- Objects associated with the elevator
local clickDetector = script.Parent.ClickDetector
local elevator_left_door = script.Parent.Parent.elevator_door1
local elevator_right_door = script.Parent.Parent.elevator_door2

-- The wall in front of the elevator that will disappear when the elevator is opened
local wall_in_front_of_elevator = script.Parent.Parent.wall_initial_elevator

-- Details about how the elevator door animation will be played
local tweenInfo = TweenInfo.new(
	5, --Duration in seconds
	Enum.EasingStyle.Linear, --EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	1 -- DelayTime in seconds
)


-- Open the elevator doors when the elevator button is clicked by the player
clickDetector.MouseClick:Connect(function()
	
	-- End point for the door movement
	local tween_goal_left = {Position = elevator_left_door:GetPivot().Position + Vector3.new(-4,0,0)}
	local tween_goal_right = {Position = elevator_right_door:GetPivot().Position + Vector3.new(4,0,0)}

	-- Creates an object that can move the elevator doors
	local tween_left = tweenService:Create(elevator_left_door,tweenInfo,tween_goal_left)
	local tween_right = tweenService:Create(elevator_right_door,tweenInfo,tween_goal_right)
	
	-- Make the wall in front of the elevator disappear
	wall_in_front_of_elevator.CanCollide = false
	wall_in_front_of_elevator.Transparency = 1
	for i, v in ipairs(wall_in_front_of_elevator:GetChildren()) do
		if v:IsA "Texture" then
			v.Transparency = 1
		end
	end
	
	tween_left:Play()
	tween_right:Play()
end)