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

Why doesn't coroutine.create() work and am I doing it right?

Asked by 3 years ago
Edited 3 years ago

So, I was having to use two scripts instead of one. But I wanted to just combine it into one script. Nothing works. If. you could help please, thank you. I may use this in other scripts.

Only a part of LocalScript. The rest of the LocalScript works. This is after some of the code in the LocalScript.

Code:


function create()
    local isnil = false
    repeat
    if workspace:FindFirstChild("Car") == nil and isnil == false then
        isnil = true
        wait(1)
        script.Parent.Parent.screen.Frame2:TweenPosition(UDim2.new(0,0,0,0),nil,nil,0.5)
        script.Parent.Parent.screen.Frame2.Visible = true
    end
    wait(1)
    until not workspace:FindFirstChild("Car") == nil
end

local co = coroutine.create(create)

Thanks, b_mni

2 answers

Log in to vote
1
Answered by
Sulu710 142
3 years ago

In this scenario, you are creating the coroutine but not running it. There are two ways you can go about this:

1.

The line local co = coroutine.create(create) creates the coroutine. This basically means you are creating the coroutine, or thread, and storing it in the variable co. To then run the coroutine you created, use coroutine.resume.

function create()
    local isnil = false
    repeat
    if workspace:FindFirstChild("Car") == nil and isnil == false then
        isnil = true
        wait(1)
        script.Parent.Parent.screen.Frame2:TweenPosition(UDim2.new(0,0,0,0),nil,nil,0.5)
        script.Parent.Parent.screen.Frame2.Visible = true
    end
    wait(1)
    until not workspace:FindFirstChild("Car") == nil
end

local co = coroutine.create(create) -- Creates the coroutine with the code in the above function

coroutine.resume(co) -- This runs, or resumes the coroutine

2.

The second thing you can do is use a handy function called coroutine.wrap. This will create a coroutine and store it in a variable, with the function you want to run when the coroutine is called as the only parameter. When you use coroutine.wrap to create a coroutine, you then have to simply call it as you would any normal function.

local co = coroutine.wrap(function() -- creates the coroutine/thread with the below code as the source
    local isnil = false
    repeat
    if workspace:FindFirstChild("Car") == nil and isnil == false then
        isnil = true
        wait(1)
        script.Parent.Parent.screen.Frame2:TweenPosition(UDim2.new(0,0,0,0),nil,nil,0.5)
        script.Parent.Parent.screen.Frame2.Visible = true
    end
    wait(1)
    until not workspace:FindFirstChild("Car") == nil
end)

co() -- runs the thread

If you only plan to run the thread once, I would use the second option, but if you plan to use coroutine.yield and coroutine.resume often as more complex code would require, then use to first option. I hope this helped! If you need more information check out this article about coroutines: https://developer.roblox.com/en-us/articles/Beginners-Guide-to-Coroutines

Ad
Log in to vote
1
Answered by 3 years ago

I figured it out: You have to use coroutine.resume() to start it.

function create()
    local isnil = false
    repeat
    if workspace:FindFirstChild("Car") == nil and isnil == false then
        isnil = true
        wait(1)
        script.Parent.Parent.screen.Frame2:TweenPosition(UDim2.new(0,0,0,0),nil,nil,0.5)
        script.Parent.Parent.screen.Frame2.Visible = true
    end
    wait(1)
    until not workspace:FindFirstChild("Car") == nil
end

local co = coroutine.create(create)
coroutine.resume(co)
0
how can i accept my own answer lol User#29913 36 — 3y

Answer this question