For loop stacks on itself

I’m new and this is my first week scripting, so bare with me please :slight_smile:

1. What do you want to achieve? Keep it simple and clear!
A basic loop with i/10 transparency in the basic fade effect

2. What is the issue? Include screenshots / videos if possible!
The transparency i/10 is triggered multiple times with different i numbers, and so create this weird faze in and out effect.

3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
This seemed to happen when I moved a CollectionService:AddTag function from it’s own script to a modulescript, but it might not be related. I did this because because:

  1. I don’t know how to use BindableEvent lol
  2. The other scripts using CollectionService:GetTagged() ran faster than the tagging script, and so left the arrays empty. Calling from Modulescript solved this.
local CollectionService = game:GetService("CollectionService")
local myModule = require(script.Parent.ModuleScript)

myModule.addTag("FadingParts","FadingPart")
local TagArray = CollectionService:GetTagged("FadingPart") 

local function fade(part)-- basic fade effect on part
	local isTouched = false -- debounce
	if not isTouched then
		isTouched = true
		for i=1,10 do
			print(i) -- THIS block is where it bugs out! I think
			part.Transparency = i/10
			task.wait(0.1)
		end
		part.CanCollide = false
		task.wait(3)
		part.CanCollide = true
		part.Transparency = 0
		isTouched = false
	end
end

for i,v in (TagArray) do -- A loop through the "FadingPart" Array and connecting to to the function fade if touched
	v.Touched:Connect(function()
		fade(v)
	end)
end

ps. Am I overcomplicating this?

2 Likes

Yes

There are many issues with you code, you are creating a Boolean within your code, since it meets the condition automatically, it will still fire, so it will stack.

local Db = false


function Fade(part)
   -- check if its the Player
if not Db then
    Db = true
    for i = 0,1,.1 do task.wait(.1)
        Ex.Transparency = i
    end
    Db = false
    
end

Another Issue is that you are using GetTagged incorrectly, it is best if you do:

for _,i in pairs(CollectionService:GetTagged("FadingPart") do
    i.Touched:Connect(fade)
end

1 Like

Putting the debouce outside the function makes it so that only 1 touched part is looping at a time. I need multiple parts to be able to run the function when touched.

2 Likes

Then each part needs its debounce, possibly as an Attribute.

2 Likes

Am I not already doing that by having a debounce within the function?

Whenever the function fade() is called, which is only when v.Touched(aka part), doesn’t that part automatically get a debounce variable? since it’s within the function running?

That’s not how that works, He is saying to create an Attribute, so they can have their own debounce rather than a script Declaring all of them under the Debounce

You can do this:

function Fade()
if not part:GetAttribute("Debounce") then
    part:SetAttribute("Debounce", true)
    for i = 0,1,.1 do task.wait(.1)
        Ex.Transparency = i
    end
    part:SetAttribute("Debounce", false)
    
end

This code should still work if the Attribute doesnt exist, but i would recommend checking if the Attribute is false

2 Likes

Setting Attributes solves it, thanks.

1 Like

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