I created a giant part that when touched by a player, should gradually turn the limb that touched the part invisible.
script.Parent.Touched:connect(function(part) local t = 0 if part.Parent:FindFirstChild("Humanoid") then for s = 1, 10 do part.Transparency = t + 0.1 wait(0.5) end end end)
Problem is: The for loop only runs once, and the Humanoid Root Part becomes visible.
I would like to know why the for loop within the if statement is acting this way, and how to exclude the root part from getting its transparency changed.
Hey Sparking!
So, with your loop, you're editing part.Transparency to be the same number over and over again, since t
isn't being changed in your loop.
local touching_part = script.Parent local debounce = false -- A debounce is useful for keeping your touched event from running more than once at any given time and forcing your fade to go faster touching_part .Touched:connect(function(part) if(debounce == false) then debounce = true local t = 0 if(part.Parent:findFirstChild("Humanoid")) then for s= 1, 10 do part.Transparency = t + 0.1 t = t + 0.1 wait(0.5) end end debounce = false end end)
I'd also recommend that you search for the humanoid using findFirstChildOfClass
, in case your humanoid is named something different. Also, you can use your for loop to change your part's transparency a bit more easily, like so:
local touching_part = script.Parent local debounce = false touching_part .Touched:connect(function(part) if(debounce == false) then debounce = true if(part.Parent:findFirstChildOfClass("Humanoid")) then for s= 0, 1, 0.1 do part.Transparency = s wait(0.5) end end debounce = false end end)
Avoiding HumanoidRootPart
's transparency being changed is a piece of pie. Just add to your if-statement.
local touching_part = script.Parent local debounce = false touching_part .Touched:connect(function(part) if(debounce == false) then debounce = true if(part.Parent:findFirstChildOfClass("Humanoid") and part.Name ~= "HumanoidRootPart") then for s= 0, 1, 0.1 do part.Transparency = s wait(0.5) end end debounce = false end end)
There is, however, a problem with this particular solution: With the debounce there, you can't fade more than one part at a time. So then, how do we fix this? Well, there are a couple of ways to do so, but a table will suffice.
local touching_part = script.Parent local debounces = {} touching_part .Touched:connect(function(part) if(not debounces[part]) then -- Check if the part is being faded debounces[part] = true -- If it's not, go ahead and add it to the table if(part.Parent:findFirstChildOfClass("Humanoid") and part.Name ~= "HumanoidRootPart") then for s= 0, 1, 0.1 do part.Transparency = s wait(0.5) end end debounces[part] = nil -- Get rid of the debounce for this part end end)
Hope this helps! :)