How to apply a function to all clone instances?

I am trying to make a function for all instance of the same clone to follow. However, only the first clone will follow the function.

for i = 1,10 do
		 	Copies = stardust:Clone()
			Copies.Parent = game.Workspace
			Copies.Position = Vector3.new(0, 2, 170+(i*2))
end
Copies.Touched:Connect(function(partTouched)
			print("Stardusttouch")
			if db then
				Copies.Transparency = 1
				Copies.CanCollide = false
				db = false
				local xpossition = math.random(-500,500)
				local zpossition = math.random(-500,500)
				EXPs.Value = EXPs.Value + 1
				print(EXPs.Value)
				EXP.Value = EXPs.Value
				Copies.Position = Vector3.new(xpossition, 1, zpossition)
				Copies.Transparency = 0
				Copies.CanCollide = true
				db = true
     end
end)

You’re creating only one “copies” variable to only one clone.
Instead, do this (it will be even faster)


for i = 1,10 do
		 	local Copies = stardust:Clone()
			Copies.Parent = game.Workspace
			Copies.Position = Vector3.new(0, 2, 170+(i*2))
       Copies.Touched:Connect(function(partTouched)
			print("Stardusttouch")
			if db then
				Copies.Transparency = 1
				Copies.CanCollide = false
				db = false
				local xpossition = math.random(-500,500)
				local zpossition = math.random(-500,500)
				EXPs.Value = EXPs.Value + 1
				print(EXPs.Value)
				EXP.Value = EXPs.Value
				Copies.Position = Vector3.new(xpossition, 1, zpossition)
				Copies.Transparency = 0
				Copies.CanCollide = true
				db = true
     end
end)
end



The think you were doing wrong was setting the same variable (copies) to the newest clone evrytime. It was overwriting the one before . You could also make a list copies = {} and adding each clone to it with table.insert(copies, clone) and then looping trough the table with for _, clone in pairs(copies) do end.
Hope I helped :smile:

2 Likes

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