So, I want to make a part that goes up and down. I donât want to use TweenService, because the parts will get moved occasionally, I tried using TweenService and it gave me an unexpected result.
There is a trick when you can totally use tweens, that will not break when apple is moved. What you need to do is to use invisible, anchored âRootâ part, and weld the apple to that part with âMotor6Dâ weld. Then instead of tweening the apple itself, you can tween âC0â property of that weld.
All you need to remember is that when moving apple, you NEED to use Root CFrame and not position, as adjusting position breaks welds.
apple.PrimaryPart.CFrame = CFrame.new(x,y,z) --where x, y, z is the new position
Here is an example code. This works best in the LocalScript:
local CS = game:GetService("CollectionService")
local TS = game:GetService("TweenService")
for i, v in CS:GetTagged("Apple") do
while not v.PrimaryPart do
task.wait()
end
--only main root gets to be anchored
v.PrimaryPart.Anchored = false
--first root is stationary
local root = Instance.new("Part")
root.Anchored = true
root.Position = v.PrimaryPart.Position
root.Transparency = 1
root.CanCollide = false
root.Name = "Root"
root.Parent = v
--intermittent root that only hovers, but does not spin
local hoveringRoot = root:Clone()
hoveringRoot.Name = "HoveringRoot"
hoveringRoot.Anchored = false
hoveringRoot.Parent = v
--hovering weld
local hweld = Instance.new('Motor6D')
hweld.Part0 = root
hweld.Part1 = hoveringRoot
hweld.Parent = root
--spinning weld
local sweld = Instance.new('Motor6D')
sweld.Part0 = hoveringRoot
sweld.Part1 = v.PrimaryPart
sweld.Parent = hoveringRoot
--stationary root will become the new primary part
v.PrimaryPart = root
-- hovering tween is easy
-- set and forget
-- i added a random delay, so all apples do not hover in sync
task.delay(math.random(40)/10,function()
TS:Create(hweld,
TweenInfo.new(
2, --total 4 second for both ways
Enum.EasingStyle.Sine,
Enum.EasingDirection.InOut,
-1, --forever
true), --reverses
{C0 = CFrame.new() + Vector3.new(0,2,0)}):Play()
end)
task.delay(math.random(40)/10,function()
-- spin is a bit more tricky, since tweens can go maximum 180 degrees
-- you need at least 2 tweens for the part to make 360 turn
-- but I always suggest 3, so you can control the direction of the tween
--swap ANY of these tweens to switch direction, each tween is 1/3 turn
local tweens = {
TS:Create(sweld,TweenInfo.new(1,Enum.EasingStyle.Linear),{C0 = CFrame.Angles(0,math.pi*2/3,0)}),
TS:Create(sweld,TweenInfo.new(1,Enum.EasingStyle.Linear),{C0 = CFrame.Angles(0,math.pi*4/3,0)}),
TS:Create(sweld,TweenInfo.new(1,Enum.EasingStyle.Linear),{C0 = CFrame.Angles(0,0,0)}),
}
for i, tween in pairs(tweens) do
tween.Completed:Connect(function()
tweens[i%3 + 1]:Play() -- play next tween
end)
end
-- start the first tween
tweens[1]:Play()
end)
end
Hope that helps.
P.S. If you want to animate apples on client, and move them around on server (recommended), you will need to create all roots and welds on server and not on client as in this example.