I have the following code for airplane landing gear:
local TweenService = game:GetService("TweenService")
local goal2 = {}
local rot = CFrame.fromOrientation(0, 0, -90)
local pos = script.Parent.Position + Vector3.new(0, -3, 0)
goal2.CFrame = CFrame.new(pos)*rot
local tweenInfo = TweenInfo.new(
2, -- Time
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
0, -- RepeatCount (when less than zero the tween will loop indefinitely)
true, -- Reverses (tween will reverse once reaching it's goal)
0 -- DelayTime
)
local tween = TweenService:Create(script.Parent, tweenInfo, goal2)
tween:Play()
The tween is supposed to be dynamic, in which that if the plane is moving, the Tween will work and not have the landing gear move away from the plane, like what happened with my previous iteration.
However, the above script doesn’t do anything when it plays. It says the Tween is “completed” but it does nothing.
It seems that the script is not updating the goal position and orientation of the tween dynamically as the plane moves. To fix this issue, you can create a function that updates the goal position and orientation every frame using a while loop and the RenderStepped event from the RunService . Here’s how you can modify the script:
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local function updateGoal()
local rot = CFrame.fromOrientation(0, 0, -90)
local pos = script.Parent.Position + Vector3.new(0, -3, 0)
return CFrame.new(pos) * rot
end
local function createTween()
local tweenInfo = TweenInfo.new(
2, -- Time
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
0, -- RepeatCount (when less than zero the tween will loop indefinitely)
true, -- Reverses (tween will reverse once reaching it's goal)
0 -- DelayTime
)
return TweenService:Create(script.Parent, tweenInfo, {CFrame = updateGoal()})
end
local function playTween()
local tween = createTween()
tween:Play()
tween.Completed:Wait()
end
RunService.RenderStepped:Connect(function()
playTween()
end)
In this modified script, the updateGoal() function calculates the new goal position and orientation based on the current position of the plane. The createTween() function creates a new tween using the updated goal position and orientation, and the playTween() function plays the tween and waits for its completion. The script connects the playTween() function to the RenderStepped event, so it updates the goal position and orientation, and plays the tween every frame.
With this approach, the tween should work dynamically, and the landing gear should not move away from the plane.