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

How do I get two functions to run at the same time?

One of them is

		for i=0, 1, 0.01 * 100 do
model:SetPrimaryPartCFrame(CFrame.new( model.Body.Position:Lerp(CFrame.new(-0.713, 1045.29, -94.712).p, i) )* CFrame.Angles(x,math.rad(0),z))
wait(0)
end

The other is

for i = 0, 90 do
        model:SetPrimaryPartCFrame(model.Body.CFrame * CFrame.Angles(0, 2 * math.rad(i), 0))
        wait(0)
	end

How do I get them to happen at the same time in one script, rather than having one happen before the other?

2 answers

✓ Accepted answer

Use a coroutine:

coroutine.resume(coroutine.create(function()
	for i=0, 1, 0.01 * 100 do
		model:SetPrimaryPartCFrame(CFrame.new( model.Body.Position:Lerp(CFrame.new(-0.713, 		1045.29, -94.712).p, i) )* CFrame.Angles(x,math.rad(0),z))
		wait(0)
	end
end))

coroutine.resume(coroutine.create(function()
	for i = 0, 90 do
        	model:SetPrimaryPartCFrame(model.Body.CFrame * CFrame.Angles(0, 2 * math.rad(i), 0))
        	wait(0)
   	end
end))

Coroutines

4 comments

0

Coroutine.wrap is a lot more efficient.

0

Still works, and good job being an idiot and downvoting. It only proves the arrogance that surrounds you while carrying a low IQ.

0

GetSporked, did you really downvote for not using coroutine.wrap? Every time I tried coroutine.wrap it failed to work for me, Dueling posted a working method and he should be credited for it. And coroutine.wrap is not much more efficient than coroutine.resume(coroutine.create(.

0

delay(func,0) is even shorter!

use spawn(fn). Spawn is a ROBLOX-specific function that simplifies coroutines for you. It just takes a function and runs it asynchronously.

spawn(function()
    for i=0, 1, 0.01 * 100 do
        model:SetPrimaryPartCFrame(CFrame.new( model.Body.Position:Lerp(CFrame.new(-0.713,      1045.29, -94.712).p, i) )* CFrame.Angles(x,math.rad(0),z))
        wait(0)
    end
end)

spawn(function()
    for i = 0, 90 do
            model:SetPrimaryPartCFrame(model.Body.CFrame * CFrame.Angles(0, 2 * math.rad(i), 0))
            wait(0)
    end
end)

Check out the Wiki article for more info.

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