Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How come this script only turns some doors transparent?

Asked by
ovicaI 7
2 years ago

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)
0
put line 1 after line 7 greatneil80 2647 — 2y
0
Is every part of your doors Named Exactly "Door"? TGazza 1336 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago

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)

0
That worked, but now if I press X again it won't set the transparency to 0. ovicaI 7 — 2y
0
Change the transparency to 0 and CanCollide to true in the second loop BabanizPROcuk 49 — 2y
Ad
Log in to vote
0
Answered by
ovicaI 7
2 years ago

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

Answer this question