How do I edit the direction of a tween?

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?

You got to make the models starting position around ground level and then the goal position will be where it’d actually be. This will give the effect of it growing up from the ground and not the sky.

To do this before you do

T:Play()

Above this just lower the model by casting a ray down from the primary part and seeing how many studs you’d have to bring it down

--Ray
Model:SetPrimaryPartPostion(--[[floor position]])
T:Play()

This way you wont have to edit your tweens.

2 Likes

Would I also have to make a tween for getting it back to its original position then?

In addition, this is not a valid function to run on models.

The solution I ended up figuring out was setting the model’s primary part to be a brick that is at ground-level.

Look at the difference:

When primary part was set to a part at ground level:

When it was set to something above ground level:

Before doing this, I tried using that module I mentioned before to tween the size and position at the same time based on setting the primary part’s CFrame to be at ground level, but it ended up coming out like crap:

Unless someone has an even better solution, I think this is the solution.