Search code examples
lua3dphysicsrobloxtween

How to resize a cylinder from one end and move it backwards in Roblox


I'm making a cigarette in Roblox and I want it to work correctly

it made the cig resize to the front.

you can get it yourself here: https://filetransfer.io/data-package/qKBHvHxS#link

and for people who want the code all you really need to see is this:

for i = bit.Size.X, 0, -0.1 do
    local tweenInfo = TweenInfo.new(0.1)
    local goal = {}
    goal.Size = Vector3.new(i, bit.Size.Y, bit.Size.Z)
    local tween = TweenService:Create(bit, tweenInfo, goal)
    tween:Play()
    wait(0.1)
    bit.CFrame = bit.CFrame + bit.CFrame.LookVector * -0.1
end

But seriously, I do recommend downloading the RBXM file and actually testing it.

Also its a tool, so this accounts for rotations.


Solution

  • If you're going to use a Tween, don't put it into a for-loop. When two Tweens try to manipulate the same properties, they start to cancel each other out.

    So just put all the resizing logic into one tween and make it take the full length of time :

    local BURN_RATE = 1.0 -- studs/second
    local cigLength = bit.Size.X
    local tweenInfo = TweenInfo.new(cigLength * BURN_RATE) -- take more time based on how long the cig is
    
    local goal = {
        Size = Vector3.new(0, bit.Size.Y, bit.Size.Z),
        Position = (bit.Position - Vector3.new(cigLength / 2, 0, 0)),
    }
    local tween = TweenService:Create(bit, tweenInfo, goal)
    tween:Play()
    
    -- TODO : if the tool is put away, simply cancel the tween