Connect function acting odd

i have a loop that spawns 360 parts, when one of them gets touched, it fades away before respawning again, but its acting very odd:
https://gyazo.com/08497a3c58cbee3c8c82a7f80e671aed

it takes a while before cancollide is false for whatever reason
and then starts to blink. why?

for i = 1,360 do

         local part = Instance.new("Part")
	 part.Touched:Connect(function()
		for i=1,100 do
			part.Transparency = part.Transparency + 0.01
			wait()
		end
		print("done")
		part.CanCollide = false
		wait(2)
		part.Transparency = 0
		part.CanCollide= true
	end)
end

even though it prints"done" it takes a while to set it to cancollide

Notice how it prints “done” 25 times? The event is firing multiple times.

You’ll want to read through this tutorial:

I think I have an idea, change your for loop to a while loop and add this:

while true do
		part.Transparency = part.Transparency + 0.01


if part.Transparency == 1 then
part.CanCollide = false
break
wait()
end
print(“Finished the while loop”)
wait(2)
part.Transparency = 0
part.CanCollide = true
end
end)

Or maybe do what @Neutron_Flow said and use debounce since it might be because its firing the print event multiple times before moving on.

damn bruh that face when u been scripting for 1 year and forgot what debounce is

1 Like

you should add spaces, makes code look better
or you could call it indentation if you want

First of all i dont understand why are you creating non parented parts?

   	for i=1,100 do
   		part.Transparency = part.Transparency + 0.01 
   		wait()
   	end

You should take a look at Tween Service instead of a for loop for transparency and use debounce

local Ts = game:GetService("TweenService")
local info = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)

for i = 1,360 do
	local part = Instance.new("Part")
	local debounce = false
	part.Touched:Connect(function()
		if debounce == false then
			debounce = true
			local TransTween = Ts:Create(part,info,{Transparency = 1})
			TransTween:Play()
			wait(1)
			print("done")
			part.CanCollide = false
			wait(2)
			part.Transparency = 0
			part.CanCollide= true
			wait(1)
			debounce = false
		end
	end)
end

Hope that helps