I’ve known how to Tween models for a while, however I’m aware that I’m definitely not doing it properly. I’ve decided to try to change the way it works as I’m now building a tram which moves between 3 stations on a time delay. I have the script working, however I’m aware that the model Tweening function is bad practice.
Currently, I create a CFrame value, parent it to the model’s primary part, create a :GetPropertyChangedSignal(“Value”) hook to do :SetPrimaryPartCFrame every time it is triggered, then Tween the primary part and then destroy the CFrame Value once the tween is complete. Now, it works, however it’s probably the worst way I could have done it, and now that I’m trying to move a larger model I need a better, less laggy way of doing it.
So far, I’ve thought of maybe welding the model’s descendants to the primary part (if they are a union operation, mesh part or part) and then Tweening the primary part as normal, however I’m unsure as to whether this is the best way forward?
[ Bear in mind the model has doors which need to open/close! ]
If anybody has a better way I could do this, I’d really appreciate it. Thanks!
Here’s my current script:
local function TweenModel(Model, ToGo, Time)
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(Time, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)
local TP = {CFrame = ToGo}
local P = Model.PrimaryPart
if not P then
warn("Model needs a primary part in order to be tweened.")
return end
end
local CValue = Instance.new("CFrameValue")
CValue.Parent = P
CValue:GetPropertyChangedSignal("Value"):Connect(function()
Model:SetPrimaryPartCFrame(Model:GetPrimaryPartCFrame())
end)
local Tween = TS:Create(P, TI, TP)
Tween:Play()
Tween.Completed:Connect(function()
CValue:Destroy()
end)
end)