Working on a tween system for models that are bought in a tycoon:
I use this module for handling the module tweens.
Works well, except that I don’t like how the model tweens “down” from the sky, instead of “up” from the ground. How can I change this?
Here’s the code:
local button = script.Parent
local plrs = game:GetService("Players")
local module = require(workspace.TMmoduleV2)
local info = TweenInfo.new(.25,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)
local info2 = TweenInfo.new(0.5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)
local buy = workspace.tower
local clone = buy:Clone()
buy:Destroy()
local function ScaleModel(model, scale)
local primary = model.PrimaryPart
local primaryCf = primary.CFrame
for _,v in pairs(model:GetDescendants()) do
if (v:IsA("BasePart")) then
v.Size = (v.Size * scale)
if (v ~= primary) then
v.CFrame = (primaryCf + (primaryCf:inverse() * v.Position * scale))
end
end
end
return model
end
button.Touched:Connect(function(otherPart: BasePart)
local plr = plrs:GetPlayerFromCharacter(otherPart.Parent)
if plr and button.Parent:GetAttribute("touched") ~= true then
button.Parent:SetAttribute("touched",true)
clone:ScaleTo(0.1)
clone.Parent = workspace
task.spawn(function() module.TweenModuleScale(clone,info2,10) end)
task.spawn(function() module.TweenModuleScale(button.Parent,info,0.1,true) end)
end
end)
--- here is what module.TweenModuleScale is:
local TS = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local function CalculatePosition(part1,part2)
return CFrame.new(part1.Position):ToObjectSpace(CFrame.new(part2.Position)).Position
end
function module.TweenModuleScale(Model,Tweeninfo,Size,DeleteAfterTween)
if typeof(Model) ~= "Instance" then error(Model.." isnt a instance") end
if not Model:IsA("Model") then error(Model.Name.." isnt a model") end
if not Model.PrimaryPart then Model.PrimaryPart = Model:FindFirstChildWhichIsA("BasePart") end
task.spawn(function()
local Primary = Model.PrimaryPart
local AnchorState = Primary.Anchored
local TW = TS:Create(Primary,Tweeninfo,{Size = Primary.Size * Size})
for _,v in pairs(Model:GetDescendants()) do
if v:IsA("BasePart") and v ~= Primary then
local state = v.Anchored
v.Anchored = true
local T = TS:Create(v,Tweeninfo,{Size = v.Size * Size;Position = v.Position + (CalculatePosition(Primary,v) * (Size-1))})
task.spawn(function()
TW:GetPropertyChangedSignal("PlaybackState"):Wait()
if v:FindFirstChildWhichIsA("SpecialMesh") then
local mesh = v:FindFirstChildWhichIsA("SpecialMesh")
TS:Create(mesh,Tweeninfo,{Scale = mesh.Scale * Size}):Play()
end
T:Play()
v.Anchored = state
end)
end
if v:IsA("Weld") or v:IsA("WeldConstraint") or v:IsA("ManualWeld") or v:IsA("Motor6D") then
if v.Name ~= "TweenWeld" then
v.Enabled = false
task.spawn(function()
TW.Completed:Wait()
v.Enabled = true
end)
end
end
end
TW:Play()
TW.Completed:Wait()
if DeleteAfterTween then
Model:Destroy()
end
Primary.Anchored = AnchorState
end)
return
end
The above code isn’t the greatest, but this is still in testing phase.,
How do I fix this?