Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

0

a loop inside a loop that calls another loop 11 times...

0

Oh, that makes sense. Is there a way I could reference the ten parts being created in the for loop outside of it?

1 answer

✓ Accepted answer

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.

1 comment

0

Thank you, this seems to have worked!

This discussion is preserved as it was. New answers, comments, and votes are closed.