In this case, I feel like TweenService would result in a smooth, good-performance finish with much less lines of codes (Can tell you how to do that if you want).
But in this code, where ever it says:
if g[i].className == "Part" then
Try using this instead:
if g[i]:IsA("BasePart") then
BasePart refers to every kind of part - wedges, unions, spawnlocations, and etc.
First, you have to set a primary part in the properties of the model you are tweening. To do this create part, and scale it to the size of the model (try your best with this process using the blue selection box/outline roblox gives when you hover over the model), Set the transparency of this part to 1 (invisble), set the cancollide to false. Then, name this part "Primary" and parent it to the model. Then, you can select this part as the PrimaryPart of the model you're tweening.
Make sure to unanchor all the parts in the model.
Then only anchor the PrimaryPart.
Second, you have to create welds between the primary part and all the other parts of that model.
Personally, I use this code for welding.
local model = workspace["Model"]
for i,v in pairs(model:GetDescendants()) do
if v:IsA("BasePart") then
local Weld = Instance.new("WeldConstraint")
Weld.Parent = v
Weld.Part0 = v
Weld.Part1 = model.PrimaryPart
end
end
Like the comments say, replace the model variable with the location of the model you're tweening in the workspace.
Then, run this code in the command bar at the bottom of roblox studio - to activate command bar if it's not there, go to the view tab at the top and click on command bar.
After that, there should be WeldConstraints through all your parts attaching to the primaryPart.
All you have to do from there on is write the Tween Script. Insert a script into the model and type the following:
local TweenService = game:GetService("TweenService")
local Model = script.Parent
local PrimaryPart = Model.PrimaryPart
local ActiveValue = Model.Active
local EventDebounce = false
local ModelTweenInfo = TweenInfo.new(
3,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false,
0
)
local OpenTween = TweenService:Create(PrimaryPart, ModelTweenInfo, {CFrame = PrimaryPart.CFrame * CFrame.new(0, 0, 10)})
local CloseTween = TweenService:Create(PrimaryPart, ModelTweenInfo, {CFrame = PrimaryPart.CFrame * CFrame.new(0, 0, 0)})
ActiveValue.Changed:Connect(function()
if ActiveValue.Value == true then
OpenTween:Play()
else
CloseTween:Play()
end
end)
Without the comments and open formatting the code should appear much shorter.
local TweenService = game:GetService("TweenService")
local Model = script.Parent
local PrimaryPart = Model.PrimaryPart
local ActiveValue = Model.Active
local EventDebounce = false
local ModelTweenInfo = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
local OpenTween = TweenService:Create(PrimaryPart, ModelTweenInfo, {CFrame = PrimaryPart.CFrame * CFrame.new(0, 0, 10)})
local CloseTween = TweenService:Create(PrimaryPart, ModelTweenInfo, {CFrame = PrimaryPart.CFrame * CFrame.new(0, 0, 0)})
ActiveValue.Changed:Connect(function()
if ActiveValue.Value == true then
OpenTween:Play()
else
CloseTween:Play()
end
end)
I tried to make it as adjustible to your model as possible - if you give me a screenshot of how your model is layed out, I may be able to specify the code more. But, this is the final result should be smooth and very adaptable.
4 comments
Hello, yea please tell me how to do it with Tween.
And thank you, it worked!
Alright, its a bit tedious to explain, so I will try my best.
Here you go - let me know if you run into any issues!