Help with collectionservice

  1. What do you want to achieve?
    Make leaves move
  2. What is the issue?
    script fires multipie times depend on amount of leave part
  3. What solutions have you tried so far?
    I tried to slove it via forum,but no progress yet
local collectionservice = game:GetService("CollectionService")

local Leave = collectionservice:GetTagged("Leaves")

for _,v in pairs(Leave) do

	local leaves = v
	print(leaves)
	v.Position = Vector3.new(v.Position.x, v.Position.y-0.2, v.Position.z)
	x = 0
	z = 0
	T = -99999
	tall = leaves.Size.Y / 2
	math.randomseed(tick())
	rand = (math.random(0,20))/10
	task.spawn(function()
		
	while true do
		x = leaves.Position.x + (math.sin(T + (leaves.Position.x/5)) * math.sin(T/9))/10
		z = leaves.Position.z + (math.sin(T + (leaves.Position.z/9)) * math.sin(T/18))/6
		leaves.CFrame =
				CFrame.new(x, leaves.Position.y, z) * CFrame.Angles((z-leaves.Position.z)/tall, 0,(x-leaves.Position.x)/-tall)
		wait()
		T = T + 0.12
	end
	end)
end

Put everything in a function and call it passing each leaf as a parameter:

local CollectionService = game:GetService("CollectionService")
local leaves = CollectionService:GetTagged("Leaves")

local function moveleaf(leaf)
    local x, z, T = 0, 0, -99999
    local tall = leaf.Size.Y / 2
    leaf.Position = Vector3.new(leaf.Position.x, leaf.Position.y-0.2, leaf.Position.z)
    math.randomseed(tick())
    local rand = (math.random(0, 20))/10

    task.spawn(function()
        while true do
            x = leaf.Position.x + (math.sin(T + (leaf.Position.x / 5)) * math.sin(T / 9)) / 10
            z = leaf.Position.z + (math.sin(T + (leaf.Position.z / 9)) * math.sin(T / 18)) / 6
            leaf.CFrame = CFrame.new(x, leaf.Position.y, z) * CFrame.Angles((z - leaf.Position.z) / tall, 0, (x - leaf.Position.x) / -tall)
           task.wait()
            T = T + 0.12
        end
    end)
end

for _, leaf in pairs(leaves) do
    moveleaf(leaf)
end
2 Likes

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