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

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

Asked by
Zerio920 285 Moderation Voter
10 years ago

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

Log in to vote
3
Answered by
Lacryma 548 Moderation Voter
10 years ago

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

0
Coroutine.wrap is a lot more efficient. GetSporked 5 — 10y
0
Still works, and good job being an idiot and downvoting. It only proves the arrogance that surrounds you while carrying a low IQ. Lacryma 548 — 10y
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(. M39a9am3R 3210 — 10y
0
`delay(func,0)` is even shorter! BlueTaslem 18071 — 10y
Ad
Log in to vote
1
Answered by 10 years ago

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.

Answer this question