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
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.