How to make parts transparent when touched

I want to locally make specific parts inside of a folder in the workspace turn transparent when you touch it

I have this in a LocalScript but my code doesnt work can someone help me fix it

local Part = game.Workspace.GuessParts:GetDescendants()

Debounce = false

for i, parts in pairs(Part) do

	if parts:IsA("Part") then
		
		parts.Touched:Connect(function(hit)
			
			local hum = hit.Parent:FindFirstChild("Humanoid")
			
			if hum then
				
				Debounce = true

				parts.Transparency = 0.7
				parts.CanCollide = false

				wait(10)

				parts.Transparency = 0
				parts.CanCollide = true

				Debounce = false
			end
		end)
	end
end
1 Like

getdescendants() gets a table with all the instances in it so doing

wont work on it, you need to loop through all the instances first:

local Part = game.Workspace.GuessParts
Debounce = false

Part.Touched:Connect(function(Touched)

	if Touched.Parent:FindFirstChild("Humanoid") then --// this is how you check if its a character that touched the part.
		Debounce = true
		for i,v in pairs(Part:GetDescendants()) do 
			task.spawn(function() --// without this the script would wait 10 seconds between changing every part.
				v.Transparency = 0.7
				v.CanCollide = false
		
				task.wait(10)
		
				v.Transparency = 0
				v.CanCollide = true
			end)
		end
		Debounce = false
	end
end)

local Part = game.Workspace.GuessParts:GetDescendants()

Debounce = false

for i, parts in pairs(Part) do

  if parts:IsA(“Part”) then
     parts.Touched:Connect(function(hit)
      local hum = hit.Parent:FindFirstChild(“Humanoid”)
       if hum then
		Debounce = true

		parts.Transparency = 0.7
		parts.CanCollide = false

		wait(10)

		parts.Transparency = 0
		parts.CanCollide = true
		
		Debounce = false
     end
   end
 end)
end


Wrote this in mobile so there might be mistakes and if it’s not formatted correctly then fix it. Report any mistakes that you find and I’ll try to fix it

1 Like

it doesnt work and this was in the output:

it said Touched was not part of the folder

1 Like

It’s a folder so that’s why they use descendants

1 Like

Try my solution instead because that might fix the problem

it didnt work is the script right

im so confused nothing is working i just want to change properties of parts when touched

1 Like

What error does it display? I Can fix it, oh wait I may know why. I saw an error in my script.

Try my script now. It should be fixed

why

1 Like

Ok I’ll get on my computer and help you. Give me a minute

1 Like

i have this code and its not working and nothing in the output

local Part = game.Workspace.GuessParts:GetDescendants()

Debounce = false

for i, parts in pairs(Part) do

	if parts:IsA("Part") then
		
		parts.Touched:Connect(function(hit)
			
			local hum = hit.Parent:FindFirstChild("Humanoid")
			
			if hum then
				
				Debounce = true

				parts.Transparency = 0.7
				parts.CanCollide = false

				wait(10)

				parts.Transparency = 0
				parts.CanCollide = true

				Debounce = false
			end
		end)
	end
end
1 Like

I have the code right here. Let me paste it in

(Laptop shut down due to low battery give me a minute)

1 Like

Hold on the loop isn’t running for some reason. Fixed it. However, the reason it only works on 1 part because of your debounce. So I’ll fix that real quick.

local Parts = workspace.GuessParts:GetDescendants()
local db = false
local function AssignProperty(property, value)
    for _, v in next, Parts do
        if v.ClassName == "Part" then
            v[property] = value
        end
    end 
end

for _, v in next, Parts do
    if v.ClassName == "Part" then
        v.Touched:Connect(function(h)
            if h.Parent:FindFirstChild("Humanoid") and db == false then
                db = true
                AssignProperty("Transparency", 0.7)
                AssignProperty("CanCollide", false)
                task.wait(10)
                AssignProperty("Transparency", 0)
                AssignProperty("CanCollide", true)
                db = false
            end
        end)
    end
end

Tried writing it directly in the textbox, but it should work.

That’s a lot of code for a simple thing not going to lie

wait(1) -- for the loop to run
for i, parts in pairs(workspace.GuessParts:GetDescendants()) do
	if parts:IsA("BasePart") then
		parts.Touched:Connect(function(hit)
			local Debounce = false
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum then
				if Debounce == false then
					Debounce = true
				
					parts.Transparency = .7
					parts.CanCollide = false
					task.wait(10)
					parts.Transparency = 0
					parts.CanCollide = true
				
					Debounce = false
				end
				
			end
			
		end)		
	end
	
end

Here’s your code. It’s fix and tested already. Also place it under StarterPlayerScripts

image
Both of our code has the same lines, just different.
I only added one function that makes the code itself easier to view and is flexible with customization aswell.

Optimized too which uses the classname property instead of the old IsA method, since it’s known to have some longer delays when dealing with a vast amount of parts.

I would say lines doesn’t matter. Although, I agree, it’s easier to view however, we have to take into account, this guy is a beginner. They would have a harder time to read the code and understand it.