I have a code door that should work after a code on a keypad is inputted correctly, when the code is inputted correctly, the main attribute of the door that makes sure it is a door which can be opened is set from false to true. The issue arises when the attribute is set to true after the game opens. For some reason, the door does nothing when I use the proximity prompt to open it. When I set the attribute to true before testing, it works perfectly fine, but when the attribute isn’t set to true before testing and I do the code, the script doesn’t seem to register that particular door within the script.
Video below shows what I mean:
Video
The door runs on a server script that handles all the doors in the game within one script, and it checks for an instance with the Attribute “Door”, but for some reason, if I change the attribute of a door while the game runs, it doesn’t work. So I am quite stumped at this problem and haven’t a clue on why it happens since I don’t get an error telling me why it’s not working.
local TweenService = game:GetService("TweenService")
for i, v in pairs(game.Workspace:GetDescendants()) do
if v:GetAttribute("Door") then
local open = false
local debounce = false
local prompt = v.Main.DoorInteract
local hinge = v.Hinge
local currentCFrame = hinge.CFrame
local Sound = v.Main.DoorOpen
local goal
local Tweeninfo = TweenInfo.new(
0.6,
Enum.EasingStyle.Quad,
Enum.EasingDirection.Out
)
local function Interact(IsNPC)
if not debounce then
debounce = true
open = not open -- changes true to false, false to true
if open then
print("Opening")
goal = {CFrame = currentCFrame * CFrame.Angles(0, math.rad(90), 0)}
v.Main.CanCollide = false
for _, p in pairs(v.Main:GetChildren()) do
if p.Name == "Part" then
p.CanCollide = false
end
end
else
print("Closing")
goal = {CFrame = currentCFrame * CFrame.Angles(0, math.rad(0), 0)}
v.Main.CanCollide = true
for _, p in pairs(v.Main:GetChildren()) do
if p.Name == "Part" then
p.CanCollide = true
end
end
end
local tween = TweenService:Create(hinge, Tweeninfo, goal)
tween:Play()
Sound:Play()
task.wait(.7)
debounce = false
if IsNPC and open then Interact(true) end -- If NPC interacted and the door was left open, then close it
end
end
v.Main.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("NPC") then
Interact(true)
end
end)
prompt.Triggered:Connect(function()
Interact()
end)
end
end
Any help would be appreciated, please.