Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How come this script only turns some doors transparent?

All the doors are in the same folder. All the doors have the same name, and part class. Why is this only turning some doors transparency?

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
						v.Transparency = .5
						v.CanCollide = false
						C = true
					elseif C == true then
						v.Transparency = 0
						v.CanCollide = true
						C = false
					end
					end
				end
			end
		end
		end)

2 comments

0

put line 1 after line 7

0

Is every part of your doors Named Exactly "Door"?

2 answers

You are changing C after each door so even when a door is closed the script closes is again causing some of them to not open at all. You should set C after the loop is done to make sure all doors are open/closed.

Example:

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
		if C == false then
			for i,v in pairs(doors:GetDescendants()) do
				if v:IsA("BasePart") then
					if v.Name == "Door" then
						v.Transparency = .5
						v.CanCollide = false
					end
				end
			end
			C = true
		elseif C == true then
			for i,v in pairs(doors:GetDescendants()) do
				if v:IsA("BasePart") then
					if v.Name == "Door" then
						v.Transparency = .5
						v.CanCollide = false
					end
				end
			end
			C = false
		end
	end
end)

2 comments

0

That worked, but now if I press X again it won't set the transparency to 0.

0

Change the transparency to 0 and CanCollide to true in the second loop

That worked, but now if I press X again it won't set the transparency to 0.

This discussion is preserved as it was. New answers, comments, and votes are closed.