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 ;-;
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
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 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.
‘’local prompt = script.Parent
local part = workspace.Switch.Off
local part2 = workspace.Switch.On
local part3 = workspace.HardcoreRoom[“Secret Entrance”]
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)