Why wont multiple parts go invisible under the same name?

I’m trying to make a script that can make multiple parts of the same name go invisible after using a Proximity Prompt, but it’s just not working… I’ve tried renaming parts to “Off2” and including it, but that doesn’t work either.

Sorry if this is a dumb question, as I’m relatively new to coding ;-;

This is the script:

local prompt = script.Parent
local part = workspace.Switch.Off
local part2 = workspace.Switch.On
local part3 = workspace.HardcoreRoom["Secret Entrance"]

prompt.Triggered:Connect(function(player)
	part.Transparency = 1 
	part.CanCollide = false
	part3.Transparency = 1 
	part3.CanCollide = false
	part2.Transparency = 0 
	part2.CanCollide = true
	prompt.Enabled = false
end)
4 Likes

If you want to adjust multiple parts at once, you would need to use a for loop.
For example:

for index, obj in workspace.Switch:GetChildren() do -- GetChildren gets all objects parented to (in this case) the Switch
	-- Process it here
end

As you want to turn them transparent if the name is a particular thing, you would need to have an if statement within the for loop. You can expand the if by having an elseif clause to process multiple names at once. For example:

for index, obj in workspace.Switch:GetChildren() do -- GetChildren gets all objects parented to (in this case) the Switch
	if obj.Name == "On" then
		obj.Transparency = 0
		obj.CanCollide = true
	elseif obj.Name == "Off" then
		obj.Transparency = 1
		obj.CanCollide = false
	end
end
1 Like

you should do what @Seargent mentioned, but you could also use tags & use CollectionService, and control all switches in one script if you dont wanna put all ur switches under one folder. But, for beginners, you can just use :GetChildren() ig

local part2 = workspace.Switch.On only refers to one part, in this case probably the first part it can find with the name “On”

like the others said, you should use a for loop to go through the parts you want to un-invisible

just wanted to add a bit of explanation

You gotta loop through them, check if thats their name then make them invisible.

local targetName = "Off2"

--For each part under workspace (and subfolders/models)
for _, part in pairs(workspace:GetDescendants()) do
	--Check if the name matches "Off2" and that it is a part
	if part.Name ~= targetName or not part:IsA("BasePart") then continue end
	--If those conditions are met, make it transparent and non-collidable
	part.Transparency = 1
	part.CanCollide = false
end

On top of this if you want to avoid calling GetDescendants() multiple time you can either call this once and store the found parts in an array or mark them using collection service, assuming of course that they wont change mid-game (no new parts you want to control the transparency of will be added/removed). If you want dynamic behavior however you can keep what I did above which is the simple beginner friendly solution.

What is the error you’re getting.. can’t find it or it’s nil..
You’ll need WaitForChild on the paths.

I understand why you need player there, and it don’t need to be used within the fuction, however is this a server script?

prompt.triggered:Connect(function(player).

Shouldn’t this be at the end of the script?

Yes, and it is in his org script.

‘’local prompt = script.Parent
local part = workspace.Switch.Off
local part2 = workspace.Switch.On
local part3 = workspace.HardcoreRoom[“Secret Entrance”]

prompt.Triggered:Connect(function(player)
part.Transparency = 1
part.CanCollide = false
part3.Transparency = 1
part3.CanCollide = false
part2.Transparency = 0
part2.CanCollide = true
prompt.Enabled = false
end)

You sure?

I’m never sure unless I see something for myself .. :rofl:
His org post script:

local prompt = script.Parent
local part = workspace.Switch.Off
local part2 = workspace.Switch.On
local part3 = workspace.HardcoreRoom["Secret Entrance"]

prompt.Triggered:Connect(function(player)
	part.Transparency = 1 
	part.CanCollide = false
	part3.Transparency = 1 
	part3.CanCollide = false
	part2.Transparency = 0 
	part2.CanCollide = true
	prompt.Enabled = false
end)

Something has to be there, don’t matter if it used.. like player in this case.
That should also be a ServerScrtipt

Can you please give us more context? Just so I understand it as much as possible because there are a lot of things that can affect this.

Well you know you can wright a ServerScript that can do that.. the prompt at that point is just a trigger. You don’t want it to be for a individual player anyways.

Actual test

ServerScript is inside the prompt in Part6.

local prompt = script.Parent 
local part1 = workspace:WaitForChild("Part1")
local part2 = workspace:WaitForChild("Part2")
local part3 = workspace:WaitForChild("Part3")
local part4 = workspace:WaitForChild("Part4")
local part5 = workspace:WaitForChild("Part5")
local tog = true

prompt.Triggered:Connect(function(player)
	tog = not tog

	part1.Transparency = (not tog and 1 or 0)
	part2.Transparency = (not tog and 1 or 0)
	part3.Transparency = (not tog and 1 or 0)
	part4.Transparency = (not tog and 1 or 0)
	part5.Transparency = (not tog and 1 or 0)
end)

Now I’m sure.