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
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
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
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.
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
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.