How do I send variables or values from a script to a module script before it is ran?

I’m trying to make a door system ran by a module script, so If I ever need to make edits, I only have to change one script. I’m using proximity prompts to open the door, it is detected by a script inside of the door which also checks if the player has clearance to open that specific door. After it checks the clearance, it requires the module from replicated storage and runs the function. I use script.Parent from the perspective of the script it is required from, but it doesn’t work.

Detection script inside of the door:
debounce = false

script.Parent.GuiPart1.Open.Triggered:Connect(function(player)
	if debounce == false then
		debounce = true
		local playerClearance = player.Clearance.Value
		local playerTeam = player.Team.Name
		if script.Parent.Config.Clearance:FindFirstChild(playerClearance).Value == true or script.Parent.Config.Team:FindFirstChild(playerTeam).Value == true then
			local module = require(game.ReplicatedStorage.Modules.DoorModule)
			module.openSwingDoor1()
		end
		wait(script.Parent.Config.OpenTime.Value + 1)
		debounce = false
	end
end)

script.Parent.GuiPart2.Open.Triggered:Connect(function(player)
	if debounce == false then
		debounce = true
		local playerClearance = player.Clearance.Value
		local playerTeam = player.Team.Name
		if script.Parent.Config.Clearance:FindFirstChild(playerClearance).Value == true or script.Parent.Config.Team:FindFirstChild(playerTeam).Value == true then
			local module = require(game.ReplicatedStorage.Modules.DoorModule)
			module.openSwingDoor2()
		end
		wait(script.Parent.Config.OpenTime.Value + 1)
		debounce = false
	end
end)

This is the module script:

local module = {}

module.openSwingDoor1 = function()
	repeat
		wait()
	until script.Parent:FindFirstChild("Config")
	wait()
	script.Parent.GuiPart1.Open.Enabled = false
	script.Parent.GuiPart2.Open.Enabled = false
	script.Parent.CR1.LightPart.Color = Color3.fromRGB(39, 186, 5)
	script.Parent.CR2.LightPart.Color = Color3.fromRGB(39, 186, 5)
	tweenService = game:GetService("TweenService")
	primaryPart = script.Parent.Door.PrimaryPart
	CF = Instance.new("CFrameValue")
	CF.Value = script.Parent.Door.PrimaryPart.CFrame
	CF.Changed:Connect(function()
		primaryPart.CFrame = CF.Value
	end)

	local TurnValue
	local TurnBackValue

	TurnValue = 130
	TurnBackValue = -130

	local tween = tweenService:Create(CF, TweenInfo.new(2,Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {Value = primaryPart.CFrame * CFrame.Angles(math.rad(0),math.rad(TurnValue),math.rad(0))}):Play()
	script.Parent.AudioPart.OpenFX:Play()
	wait(script.Parent.Config.OpenTime.Value)
	local tween = tweenService:Create(CF, TweenInfo.new(2,Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {Value = primaryPart.CFrame * CFrame.Angles(math.rad(0),math.rad(TurnBackValue),math.rad(0))}):Play()
	wait(0.8)
	script.Parent.AudioPart.CloseFX:Play()
	script.Parent.CR1.LightPart.Color = Color3.fromRGB(255, 89, 89)
	script.Parent.CR2.LightPart.Color = Color3.fromRGB(255, 89, 89)
	wait(0.1)
	script.Parent.GuiPart1.Open.Enabled = true
	script.Parent.GuiPart2.Open.Enabled = true
end

module.openSwingDoor2 = function()
	repeat
		wait()
	until script.Parent:FindFirstChild("Config")
	wait()
	script.Parent.GuiPart1.Open.Enabled = false
	script.Parent.GuiPart2.Open.Enabled = false
	script.Parent.CR1.LightPart.Color = Color3.fromRGB(39, 186, 5)
	script.Parent.CR2.LightPart.Color = Color3.fromRGB(39, 186, 5)
	tweenService = game:GetService("TweenService")
	primaryPart = script.Parent.Door.PrimaryPart
	CF = Instance.new("CFrameValue")
	CF.Value = script.Parent.Door.PrimaryPart.CFrame
	CF.Changed:Connect(function()
		primaryPart.CFrame = CF.Value
	end)

	local TurnValue
	local TurnBackValue

	TurnValue = -130
	TurnBackValue = 130

	local tween = tweenService:Create(CF, TweenInfo.new(2,Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {Value = primaryPart.CFrame * CFrame.Angles(math.rad(0),math.rad(TurnValue),math.rad(0))}):Play()
	script.Parent.AudioPart.OpenFX:Play()
	wait(script.Parent.Config.OpenTime.Value)
	local tween = tweenService:Create(CF, TweenInfo.new(2,Enum.EasingStyle.Quart, Enum.EasingDirection.Out), {Value = primaryPart.CFrame * CFrame.Angles(math.rad(0),math.rad(TurnBackValue),math.rad(0))}):Play()
	wait(0.8)
	script.Parent.AudioPart.CloseFX:Play()
	script.Parent.CR1.LightPart.Color = Color3.fromRGB(255, 89, 89)
	script.Parent.CR2.LightPart.Color = Color3.fromRGB(255, 89, 89)
	wait(0.1)
	script.Parent.GuiPart1.Open.Enabled = true
	script.Parent.GuiPart2.Open.Enabled = true
end
	
return module

“CR” stands for CardReader.
“GuiPart” is the part which has the proximitypromt inside.

Thanks a lot, any replies would help!

Cosmo

1 Like

Are you talking about parameters?
Like sending values to a function so you can use them in it?

If so, read about functions, there’s a parameters section.

Yeah, I learnt that if you use script.Parent in a module script it’ll refer to the module scripts parent which is usually replicated storage hence it won’t work.

To fix this consider using collection service Instance added event which returns the instance you tag it with which is the door part/model. There is an example script there check it out. Plus you can continually clone doors and have them work.

Otherwise you can add a parameter to the open door function and replace script.Parent with the parameter.

module.openSwingDoor1 = function(door)
	door.GuiPart1.Open.Enabled = false--ex instead of script.Parent

--then in the normal script
			module.openSwingDoor2(script.Parent)
1 Like

Isn’t that sending information from a module script to a script, not a script to a module script? Give me a second to try dthecoolest’ reply.

1 Like

I am pretty sure not, you can send parameters from a script to module script, just like calling a function.
For instance:

local modRequire = nil --Imagine there was a module required here
modRequire.func(3,5) --Send 3 and 5 as parameters

Module:

local mod = {}
mod.func = function(num1, num2)
    print(num1, num2) -- 3 and 5
end

return mod

From a module script to a script is return, not parameters

2 Likes

Thanks for the solution, also thanks @LightningLion58 for clarifying it to me! I wasn’t aware you could use params for module scripts.

1 Like