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

How do I get to loops running at the same time changing CFrame?

Hi, I'm trying to make a script that will creates multiple bricks at random positions that move upwards after being created. I've managed to succeed with the first part, but I have no idea how to make the bricks move up after being created.

I've tried using coroutines to make two loops work together, but it didn't work well at all and the bricks moved upwards slower over time and became less smooth. Here's a video showing the problem.

I'm really stumped by this so any help would be appreciated, thanks!

Here's the code:

local debris = game:GetService("Debris")

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

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, 0, 0)) + Vector3.new(rand(-num, num), rand(-num, num), rand(-num, num))))
		debris:AddItem(brick, 0.75)
		
		coroutine.resume(coroutine.create(function() --part that moves bricks up
			while true do
			brick.CFrame = brick.CFrame * CFrame.new(0, 0.1, 0)
			wait()
			end
		end))
	end
	wait()
end

EDIT: After numerous replies which haven't addressed the problem, I think I phrased my problem extremely poorly, which I apologize for. Even while trying to thread the two loops, the bricks very quickly lose speed and move extremely roughly.

3 answers

you need a bodyForce on those bricks so u need to do: local force = Instance.new("BodyForce") force.Parent = brick force.Force = Vector3.new(0, 1200, 0)--a newly created block floats at 1100

3 comments

0

also change brick.Anchored to false or they wont move up

0

Hi, thanks for the reply. Is there no way to make them move with CFrame?

0

no cframe only changes the position, it doesnt move in the way u want(the way i think u want)

spawn(function()

1 comment

0

Thank you, however this didn't solve the problem. Even if I use spawn the bricks still slow down in speed very quickly. I don't know why this happens.

Hi, based on the answer from some brainlet telling you to change your methods, I've got a better solution. Coroutines, while Roblox Lua doesn't let you switch Lua states, you can start coroutines which is equivalent to starting new threads in programming languages like Javascript, etc.

https://developer.roblox.com/api-reference/lua-docs/coroutine

Best method for you right now, as it's obvious you want to stick with CFrame.

4 comments

0

In the code I posted I used coroutines, however it didn't work well, although it's likely something to do with my code.

0

Try debugging VIA print()

0

hey be respectfull, dont call me a brainlet

0

There is nothing to debug, the code runs without error however as I've said it gets slower and less smooth very quickly.

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