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

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

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

✓ Accepted answer

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

1 comment

0

Thank you!

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