Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to correct position with scaling in a script?

Asked by 2 years ago

I have a script that makes a plant grow, and currently the script makes the bottom part, the primary part, always be touching the floor/baseplate. But that only works when it is already touching the baseplate. Moving the plant up causes it to reposition upwards more than it should. Any help?

function resizeModel(model,a)
    local base = model.PrimaryPart.Position
    for _,part in pairs(model:GetDescendants()) do
        if part:IsA("BasePart") then
            part.Position = base:Lerp(part.Position,a) 
            part.Size *= a
        end
    end
end

local ss = game.ServerStorage


while true do
    wait(0.01)
    while script.Parent.PrimaryPart.Size.X <  game.ServerStorage:FindFirstChild(script.Parent.Name).PrimaryPart.Size.X * 4  do -- this is running until the model hits approx 10 times its original size
        wait(.01)

        resizeModel(script.Parent, 1 + ( game.ServerStorage:FindFirstChild(script.Parent.Name).PrimaryPart.Size.X / 100 )) -- this is actually resizing the model
        local offset = 1 + game.ServerStorage:FindFirstChild(script.Parent.Name).PrimaryPart.Size.X * .01


        script.Parent:PivotTo(CFrame.new(script.Parent.PrimaryPart.Position.X,script.Parent.PrimaryPart.Position.Y * offset, script.Parent.PrimaryPart.Position.Z)) -- this is my base for compensating with the ground
        script.Parent:PivotTo(script.Parent:GetPivot() * CFrame.Angles(0, math.rad(0), 0))
        --                                                                          ^ angle of rotation for the plant
    end
end

Answer this question