Why is this script only setting some doors transparency?

  1. What do you want to achieve? Keep it simple and clear!
    This script is supposed to change the transparency when X is pressed, and then set the transparency back to 0 when X is pressed again.

However, only some doors change transparency? Why is this?

Code:

local C = false
local doors = game.Workspace.Doors
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(Input, gameProcessedEvent)
	if gameProcessedEvent == false and  Input.KeyCode == Enum.KeyCode.X then
			for i,v in pairs(doors:GetDescendants()) do
				if v:IsA("BasePart") then
				if v.Name == "Door" then
					if C == false then            -- X to set transparency to .5
						v.Transparency = .5
						v.CanCollide = false
						C = true
					elseif C == true then    -- X again to set transparency to 0
						v.Transparency = 0
						v.CanCollide = true
						C = false
					end
					end
				end
			end
		end
		end)

The reason is that you’re setting C to true before the loop is able to finish all of the doors. You need to change the C variable once the loop finishes all the doors.

2 Likes

With that being said, you should replace your code with this:

local C = false
local doors = game.Workspace.Doors
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(Input, gameProcessedEvent)
	if gameProcessedEvent == false and  Input.KeyCode == Enum.KeyCode.X then
			for i,v in pairs(doors:GetDescendants()) do
				if v:IsA("BasePart") then
				if v.Name == "Door" then
					if C == false then            -- X to set transparency to .5
						v.Transparency = .5
						v.CanCollide = false
					elseif C == true then    -- X again to set transparency to 0
						v.Transparency = 0
						v.CanCollide = true
					end
					end
				end
			end
                        C = not C
		end
		end)

Sorry for the bad indenting, I’m on mobile