Trying to apply one script to all parts in the map with the same tag or name

Hello! I have a large amount of trees in my map, and I want all of them to be animated.

I already have a script (credit: TAWK1215) that works if it is inserted into the foliage mesh part of the tree as a child:

pos = script.Parent.Position
pos = Vector3.new(pos.x, pos.y-0.2, pos.z)
x = 0
z = 0
T = -99999
tall = script.Parent.Size.Y / 2
math.randomseed(tick())
rand = (math.random(0,20))/10
while true do
x = pos.x + (math.sin(T + (pos.x/5)) * math.sin(T/9))/3
z = pos.z + (math.sin(T + (pos.z/6)) * math.sin(T/12))/4
script.Parent.CFrame =
	CFrame.new(x, pos.y, z) * CFrame.Angles((z-pos.z)/tall, 0,(x-pos.x)/-tall)
	wait()
	T = T + 0.12
end

However, since there’s a lot of trees, I don’t want to insert this script into each individual foliage mesh, as that would clearly worsen the game performance.

What I already tried:
I used a plugin, Tag Editor, and made my own custom tag called “foliage”, which I then applied to each foliage mesh. I then inserted this script into ServerScriptService, trying to use CollectionService as such:

local CollectionService = game:GetService("CollectionService")

for _, part in CollectionService:GetTagged("foliage") do
	pos = part.Position
	pos = Vector3.new(pos.x, pos.y-0.2, pos.z)
	x = 0
	z = 0
	T = -99999
	tall = part.Size.Y / 2
	math.randomseed(tick())
	rand = (math.random(0,20))/10
	while true do
	x = pos.x + (math.sin(T + (pos.x/5)) * math.sin(T/9))/3
	z = pos.z + (math.sin(T + (pos.z/6)) * math.sin(T/12))/4
	part.CFrame =
		CFrame.new(x, pos.y, z) * CFrame.Angles((z-pos.z)/tall, 0,(x-pos.x)/-tall)
		wait()
		T = T + 0.12
	end
end

The script runs, but it doesn’t work, the leaves still weren’t moving. I may have used it incorrectly though, I’m still new to coding :sweat_smile:

Any advice on how to make the script work, or tips for a different method I could use instead would be appreciated! I’m trying to refrain from having to put all the foliage parts in one folder, since I still want them to stay in their respective models of the full tree that also have the tree trunks in them, so that it’s still easy for me to move every individual tree around. The foliage mesh parts are all named “foliage”, so I could probably do something with their unique name too, but I’m not sure how without the use of folders.

3 Likes

Try this and see if it works

local CollectionService = game:GetService("CollectionService")


while task.wait() do
	for _, part in workspace:GetDescendants()  do
		if CollectionService:HasTag(part, "foliage") then
			pos = part.Position
			pos = Vector3.new(pos.x, pos.y-0.2, pos.z)
			x = 0
			z = 0
			T = -99999
			tall = part.Size.Y / 2
			math.randomseed(tick())
			rand = (math.random(0,20))/10
			x = pos.x + (math.sin(T + (pos.x/5)) * math.sin(T/9))/3
			z = pos.z + (math.sin(T + (pos.z/6)) * math.sin(T/12))/4
			part.CFrame =
				CFrame.new(x, pos.y, z) * CFrame.Angles((z-pos.z)/tall, 0,(x-pos.x)/-tall)
			wait()
			T = T + 0.12
		end
	end
end


2 Likes

Is there a reason you need to put it in a RunService loop?

1 Like

a while loop would do the same thing, Ive just used RS like an 10 mins ago didnt even remember about while loops

But why??

There is no need to check every frame, one time is enough.

I tried it, and it did affect the parts I needed it to, thanks! However, the parts don’t really slowly sway like they were supposed to, instead they’re glitching downwards bit by bit every half a minute and sink to the ground, and not in sync either.

could be an issue in your algorithim

Yeah probably, the math part of the script works without a problem when inserted into the parts as a child though. Maybe I should look into just having it play an animation on loop instead?

Nevermind, I ran into an amazing tutorial from DatBoiEle who had made a wind system like this for anyone to use, and it works perfectly. Props to them!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.