Using module scripts to fire same function on multiple parts

hello all, I am trying to use OOP for the first time and doing some tests, in this example some simple animations for grass. I know its super messy and doesn’t look like properly moving grass at the moment but its just for testing purposes rn.
server script VVVV

local replicated = game:GetService("ReplicatedStorage")

local grassFolder = script.Parent

local grass = require(script.Grass)

for i,v in pairs(grassFolder:GetChildren()) do
	if v:IsA("BasePart") then
		coroutine.wrap(	grass.new(v))
	end
end

Module script VVVV

local maxRot = 30

local grass = {}

function grass.new(blade)
	if blade.Position then
		local origin = {
			position = blade.Position,
			orientation = blade.Orientation
		}
		grass.move(blade,origin)
	end
end

function grass.move(blade, origin)
	while wait(.5) do
		local newRot = origin.orientation + Vector3.new(math.random(0,maxRot),math.random(0,maxRot),math.random(0,maxRot))
		for i = 1, 10 do
			wait(.1)
			blade.Orientation = (newRot)*(i/10)
		end
	end
end

return grass

it currently only animates one blade of grass, even after i tried the coroutine but if someone could tell me what im doing wrong and how to avoid it in future?

local replicated = game:GetService("ReplicatedStorage")

local grassFolder = script.Parent

local grass = require(script.Grass)

for i,v in pairs(grassFolder:GetChildren()) do
	if v:IsA("BasePart") then
		task.spawn(grass.new, v)
	end
end

This is because you were calling coroutine.wrap with the result of the function as the parameter, which is not on a separate thread.

1 Like

what exactly does that mean if you don’t mind explaining. It does work by the way so thank you.

It’s difficult to explain, but you are passing the function as a parameter. This means that the result of the function goes into the function that’s being called.

For example:
print(task.wait(1)) will return the result of task.wait(1), and also wait 1 second.
print(task.wait) will return the memory address of the function.

1 Like

Thank you, I see it was because of the parentheses after the function name. I have had problems with this in the past thank you for clarifying