Tree swaying script

I have a localscript in startercharacterscripts which runs through all children of the workspace and if they are a specific tree model, makes the leaves of it sway. It all works just fine except for the fact that it only runs it on the first tree I walk up to, I understand why this is, because the while loop will now just run forever on that particular tree unless I walk away but I need the wait() in it otherwise it breaks.

Can anyone help me figure out how I can fix this, so that any tree within the stud count will all begin to sway at once? Thanks!

local Players = game:GetService("Players")
local player = Players.LocalPlayer


while true do 
	for _, Target in pairs(game.Workspace:GetDescendants()) do
	if Target.Name == "TallPine" then
				while player:DistanceFromCharacter(Target:WaitForChild("Trunk").Position) < 80 do 
					local o = Target:WaitForChild("Leaves")
					local p = .05
					local ang, rad = CFrame.Angles, math.rad
					local sin, cos = math.sin, math.cos
					local clock = tick()
					o.CFrame = o.CFrame * ang(rad(cos(clock)*p),rad(sin(clock)*p),rad(cos(clock)*p)*p)
				wait()
			end
		end
	end
	wait(1)
end

There is an excellent and super efficient example using oct-trees made by boatbomber and CloneTrooper1019 in this post.

Wind Shake: High performance wind effect for leaves and foliage - Resources / Community Resources - DevForum | Roblox

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