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 ![]()
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.