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

When I use coroutine it says a function was expected even though there is one?

Asked by 2 years ago
Edited 2 years ago

I am using CollectionService to have my conveyor belt script in one spot, And where I learned how to use the CollectionService I looked at the comments because my original code was not working and somebody said for while loops to use coroutines. But since I have not touched coroutines until now I'm not sure how to fix this script. I was reading the coroutine API reference but I can't seem to figure out how to make this work correctly. Any help would be appreciated.

local CollectionService = game:GetService("CollectionService")

local function makeBelt(part)
    part.Velocity = part.CFrame.lookVector * 15
    wait()
end

for _, Part in pairs(CollectionService:GetTagged("Belt")) do
    print(Part)
    local Coroutine = coroutine.create(makeBelt(Part))
    coroutine.resume(Coroutine)
end

Forgot to add that this is the error I get in the output: "missing argument #1 to 'create' (function expected)"

1 answer

Log in to vote
1
Answered by
enes223 327 Moderation Voter
2 years ago

Well, you see, coroutines get functions as input, what you do is call a function and give what it returns to the input. All you need to do is enter makeBelt without the parentheses and give the part variable to the resume function, fixed script:

for _, Part in pairs(CollectionService:GetTagged("Belt")) do
    print(Part)
    local Coroutine = coroutine.create(makeBelt)
    coroutine.resume(Coroutine, Part)
end
0
Thank you! MPforfun 91 — 2y
Ad

Answer this question