Reset value after touching a part

Hi there!

I have two parts in my game and i would like to set the transparency of one part to 0.95 after touching the other part. So far this i figured out. But i am struggeling to find a way to reset the transparancy back to 0 after “release”. The part that triggers the script is a part you just walk on, and when you walk of that part the transparency resets.

my code so far:
local part = script.Parent

local otherpart = game.Workspace.Room.Lobby.Bar.Transparency.pillar

local function invisible()

otherpart.Transparency = 0.95

end

part.Touched:Connect (invisible)

Any help would be appreciated. Also i want to know if it maybe is possible to: when walk (touch) on a part, everything in a certain folder will set its transparency to 0.95. Or do i have to point out every single part / object?

With kind regards,

Hey, to detect when an object has stopped touching, you can use BasePart.TouchEnded which works similarly to touched. To set every part to transparent in a folder, you will require a for loop, see Loops, as well as Instance:GetDescendants

local function setTransparency(instance, transparency)
	if instance:IsA("Folder") then -- if it's a folder, set the transparency of it's descendants
		for _, descendant in pairs(instance:GetDescendants())do
			if descendant:IsA("BasePart") then -- make sure the descendant's class is a BasePart
				descendant.Transparency = transparency
			end
		end
	elseif instance:IsA("BasePart") then -- if it's just a part, set the transparency
		instance.Transparency = transparency
	end
end

Part.Touched:Connect(function()
	setTransparency(otherpart, 0.95)
end)

Part.TouchEnded:Connect(function()
	setTransparency(otherpart, 0)
end)
  1. The simplest way to do that is by using TouchEnded event[fires when an interaction with the part has stopped/finished].
local part = script.Parent
local otherpart = game.Workspace.Room.Lobby.Bar.Transparency.pillar



local function invisible()
	otherpart.Transparency = 0.95
end

local function back()
	otherpart.Transparency = 0
end

part.Touched:Connect (invisible)
part.TouchEnded:Connect(back)

2- If you have a folder containing items you want to apply them all, you could do:

--Here I assume 'otherpart' is the folder,containing bricks.
local part = script.Parent
local otherpart = game.Workspace.Room.Lobby.Bar.Transparency:GetChildren()



local function invisible()
	for _,part in pairs(otherpart) do
		if part:IsA("BasePart") then
			part.Transparency = 0.95
		end
	end
end

local function back()
	for _,part in pairs(otherpart) do
		if part:IsA("BasePart") then
			part.Transparency = 0
		end
	end
end

part.Touched:Connect (invisible)
part.TouchEnded:Connect(back)
4 Likes

Ma’am you’re a genius, This works

Thank you all for the solutions! I found the second solution for the whole folder is working perfectly! Thank you so much!