Help with coroutines

I scripted a door handler with an option to open and close every door on the server at once, it used to work perfectly fine, but for some reason the coroutine function I used is no longer working. Any idea?

This is the code

function InteractAll(_,plr,action)
    if Perms(plr) then
        for _,a in pairs(workspace.Doors:GetChildren()) do
            for _,v in pairs(a:GetChildren()) do
                print("Working 1")
                coroutine.wrap(function()
                    print("Working 2") -- Not working from here
                    if action == "open" then
                        if v.Values.Busy.Value == false then
                            if v.Values.Open.Value == false then
                                v.Values.Busy.Value = true
                                OpenDoor:FireAllClients(v)
                                wait(1.5)
                                v.Values.Open.Value = true
                                v.Values.Busy.Value = false
                            end
                        end
                    elseif action == "close" then
                        if v.Values.Busy.Value == false then
                            if v.Values.Open.Value == true then
                                v.Values.Busy.Value = true
                                OpenDoor:FireAllClients(v)
                                wait(1.5)
                                v.Values.Open.Value = false
                                v.Values.Busy.Value = false
                            end
                        end
                    end
                end)
            end
        end
    end
end

working - YouTube - This is a video of the code working

not working - YouTube - Here it doesn’t

try this

function InteractAll(_,plr,action)
    if Perms(plr) then
        for _,a in pairs(workspace.Doors:GetChildren()) do
            for _,v in pairs(a:GetChildren()) do
                print("Working 1")
                coroutine.wrap(function()
                    print("Working 2") -- Not working from here
                    if action == "open" then
                        if v.Values.Busy.Value == false then
                            if v.Values.Open.Value == false then
                                v.Values.Busy.Value = true
                                OpenDoor:FireAllClients(v)
                                wait(1.5)
                                v.Values.Open.Value = true
                                v.Values.Busy.Value = false
                            end
                        end
                    elseif action == "close" then
                        if v.Values.Busy.Value == false then
                            if v.Values.Open.Value == true then
                                v.Values.Busy.Value = true
                                OpenDoor:FireAllClients(v)
                                wait(1.5)
                                v.Values.Open.Value = false
                                v.Values.Busy.Value = false
                            end
                        end
                    end
                end)()
            end
        end
    end
end

you need to add a () after the function

coroutine.wrap(function()

end)()
1 Like

It worked, thanks!
I don’t know why it used to work, because I don’t remember removing those ()

1 Like

In case you needed an explanation coroutine.wrap returns a function value so the calling syntax () is required to call it.

1 Like