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

Why do these parts move slower over time in a loop?

Asked by
Reivani 11
5 years ago

Hi. I'm trying to make a script that creates parts in random positions around the camera, with those parts moving upwards.

I've succeeded in the first part, but when I try to make the parts move up in a loop, they become slower and slower, and become less smooth in a short period of time.

Here's a video showing the problem. Notice how they start off going upwards smooth and fast, but very quickly lose speed and smoothness.

Here's the code:

local debris = game:GetService("Debris")

local cam = workspace.CurrentCamera
local rand = math.random
local num = 25

while true do
    for i = 0, 10 do
        local brick = Instance.new("Part")
        brick.Anchored = true
        brick.CanCollide = false
        brick.Size = Vector3.new(1, 1, 1)
        brick.Parent = workspace
        brick.CFrame = CFrame.new(cam.CFrame.Position + (cam.CFrame:VectorToWorldSpace(Vector3.new(0, 1, 0).unit) + Vector3.new(rand(-num, num), rand(-num, num), rand(-num, num))))
        debris:AddItem(brick, 0.75)

        local function moveUp()
            while true do
                brick.CFrame = brick.CFrame * CFrame.new(0, 0.1, 0)
                wait()
            end
        end
        spawn(moveUp)

    end
    wait()
end
0
a loop inside a loop that calls another loop 11 times... hellmatic 1523 — 5y
0
Oh, that makes sense. Is there a way I could reference the ten parts being created in the for loop outside of it? Reivani 11 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

The part is still moving up even when deleted by Debris.

Check to see if the brick has a parent. If it doesn't, break the loop.

while true do
    brick.CFrame = CFrame.new()
    if (not brick.Parent) then
        break
    end
    wait()
end

Also, if you want to make it even smoother, you can use RunService.RenderStepped:Wait() to wait 1/60th of a second rather than wait() which is 1/30th.

0
Thank you, this seems to have worked! Reivani 11 — 5y
Ad

Answer this question