Trouble with variables

Situation:
In my game I have some doors that should open with a proximity prompt, so I made a script to go through all models with a door tag and give them a proximity prompt that has tweening for opening and closing. The related functions for these are in a separate module
Problem:
I need to have some variables that are defined out of the main function for cmdr, however when the function runs it errors out because it can’t see the variable, despite earlier testing
Note(s):
– Suggestions are appreciated, but please make sure to provide a solution to the original problem with them (if the problem hasn’t already been solved of course)

Scripts:

Main Script:

--[[ Services ]]--
local Services = {
	Workspace = game:GetService("Workspace");
	Players = game:GetService("Players");
	ReplicatedStorage = game:GetService("ReplicatedStorage");
}

--[[ Modules ]]--
local Module: {} = require(script.Parent.CoreConfig.Doors)

--[[ Functions ]]--
local function FindAll( Start: Instance, Class: string, Tag: string, Name: string ): {Instance}
	local List = {}
	
	for _, child in pairs(Start:GetChildren()) do
		if ( not Class or child:IsA(Class) ) and ( not Tag or child:HasTag(Tag) ) and ( not Name or child.Name == Name ) then
			table.insert( List, child )
		end
	end
	
	return List
end

--[[ Main ]]--
local Doors: {} = FindAll( Services.Workspace, "Model", "Door" )

for id: number, door: Model in pairs(Doors) do
	door:SetAttribute( "DoorId", id )
	
	if door:GetAttribute("DoorType") == 2 then
		-- Global Variables
		local OpenTween: Tween, CloseTween: Tween = Module[door:GetAttribute("DoorType")].GetTweens( door.Door, { Time = 2, EasingStyle = Enum.EasingStyle.Quad, EasingDirection = Enum.EasingDirection.In } )
		local RunningThread: thread = nil
		local Locked = false
		
		-- Variables
		local Busy = false
		local Function = Module[door:GetAttribute("DoorType")].DoorFunction
		
		-- Main
		local MainThread = coroutine.create(function()
			Function(door)
		end)
		coroutine.resume(MainThread)
		
		-- Cmdr
		script.ToggleOpen.Event:Connect(function( ID: number, Open: boolean )
			if ID ~= id then
				return
			elseif Busy then
				Instance.new("BoolValue").Parent = script.Parent.Cmdr.Commands.Developer.DoorServer
				return
			end
			
			Busy = true
			
			if Open then
				coroutine.close(RunningThread)
				CloseTween:Cancel()
				OpenTween:Play()
			else
				coroutine.close(RunningThread)
				OpenTween:Cancel()
				CloseTween:Play()
			end
			
			Busy = false
		end)
		script.ToggleLock.Event:Connect(function( ID: number, Lock: boolean )
			if ID ~= id then
				return
			elseif Busy then
				Instance.new("BoolValue").Parent = script.Parent.Cmdr.Commands.Developer.DoorServer
				return
			end

			Busy = true
			Locked = Lock
			Busy = false
		end)
		script.Fix.Event:Connect(function(ID: number)
			if ID ~= id then
				return
			elseif Busy then
				Instance.new("BoolValue").Parent = script.Parent.Cmdr.Commands.Developer.DoorServer
				return
			end

			Busy = true

			coroutine.close(RunningThread)
			coroutine.close(MainThread)
			CloseTween:Play()
			Locked = true
			RunningThread = nil
			MainThread = coroutine.create(function()
				Function(door)
			end)
			coroutine.resume(MainThread)

			Busy = false
		end)
	end
end

Module Script:

local Services = table.freeze {
	TweenService = game:GetService("TweenService");
}

local M = {
	[2] = {
		GetTweens = function( DoorPart: Part, Config: {} ): {Tween}
			local Pos = DoorPart.CFrame.Position + (DoorPart.CFrame.RightVector * 12)
			local Tween = Services.TweenService:Create( DoorPart, TweenInfo.new( Config.Time, Config.EasingStyle, Config.EasingDirection ), { CFrame=CFrame.lookAt( Pos, (Pos) + DoorPart.CFrame.LookVector, DoorPart.CFrame.UpVector ) } )
			Pos = DoorPart.CFrame.Position
			return {OpenTween = Tween, CloseTween = Services.TweenService:Create( DoorPart, TweenInfo.new( Config.Time, Config.EasingStyle, Config.EasingDirection ), { CFrame=CFrame.lookAt( Pos, (Pos) + DoorPart.CFrame.LookVector, DoorPart.CFrame.UpVector ) } )}
		end,
		
		DoorFunction = function(Door: Model)
			print("Door")
			
			-- Init
			local Prompt: ProximityPrompt = script.ProximityPrompt:Clone()
			Prompt.Parent = Door.Door
			
			-- Main
			Prompt.Triggered:Connect(function(Player: Player)
				if Locked then
					Prompt.ActionText = "Locked"
					return
				else
					Prompt.ActionText = "Open"
				end
				
				if typeof(RunningThread) == "thread" then
					coroutine.close(RunningThread)
					OpenTween:Cancel()
					CloseTween:Cancel()
				end
				
				RunningThread = coroutine.create(function()
					OpenTween:Play()
					OpenTween.Completed:Wait()
					wait(3)
					CloseTween:Play()
				end)
				
				coroutine.resume(RunningThread)
			end)
		end,
	}
}

return M

You should probably minimize the amount of code and the amount of lines you use. This is so the script can be easier to utilize, edit, and read.

This is probably why you are having trouble. After minimized, read the script and try to see if you can understand and find the problem.

I’ve completely read. I’m saying you’ll be able to understand it better once you have conceded it.

So instead of solving the problem, you’re going to tell me I need to optimize my script, then solve it?

Yes, but no. I’ll help solve it if the problem is still unclear after optimized. As a scripter, your scripts should be as conceded as possible.

They need to be optimized yes, but first you need a working version

The problem is clear, I have a variable that is defined outside of the function relating to door opening and closing, and it should be able to see it but it cannot despite me testing it before

Clear as in how you fix it.

You could still concede the script even if there is errors.

But it is the first step to provide one. You should probably concede the script so we can get to the debugging part of it.

I’m confused. Do you not know what conceding is? It means minimizing how much lines of code you use so you can find a issue and a solution, which you’ve already found an issue, but solution wise you have obviously not found.

I solved it myself

Just do something similar to this

-- MainScript
local module = require(location.of.module)
local Set_Vars, Get_Vars = module()
-- ModuleScript
return function()
	local ExampleVariables = {
		var1 = "hi";
		var2 = false;
	}
	
	local function Get_Vars(): {}
		return ExampleVariables
	end
	local function Set_Vars(Vars: {})
		ExampleVariables = Vars
	end
	
	return Set_Vars, Get_Vars
end

The lesson to be learned here is to not always expect the devforum to provide competent solutions to your problems