Code door won't Work when Attribute is set to false before testing, but does work when it is set to true before testing

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.

Probably because it’s a boolean value and such an attribute is undetectable when it is set to false.

Its detectable but since its an if statement if the value is false then of course its not going work, if he wants to just do a basic check to determine if the door attribute exist he can use a snippet of the code below

for i, v in pairs(game.Workspace:GetDescendants()) do
	if v:GetAttribute("Door") ~= nil then -- Basically saying run this code if this attribute exist whether its false or true
-- code
	end
end
1 Like

True indeed, you explained it better. It’s ‘not detectable’ within his code as it checks whether the value itself is true or false. You on the other hand only check if this value exists, making it detectable.

I just tried that, and it seems to not change anything about it, It works, but only if the attribute is there before the script starts running (Which is before the play testing in studio). And I seem to find myself confused as to why, I assume it checks once on each instance within the workspace, and if there isn’t an attribute that it looks for, it doesn’t check it again, even if the attribute is set/added after the initial check. At least, that’s what I think is happening.