Storing variables in module script

Should I store the frame, hinge and prompt variables in the Instance.new function (as shown) or as local variables in the Instance:Init() function? Also any other recommendations to improve the script would be welcome

local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.75, Enum.EasingStyle.Linear)

local Door = {}
Door.__index = Door

function Door.new(server, instance)
	local self = setmetatable({}, Door)
	self.Server = server
	self.Instance = instance
	self.Frame = instance.Frame
	self.Hinge = instance.Hinge
	self.Prompt = instance.Prompt
	self.Open = false

	return self
end

function Door:Init()
	local hingeCFrame = self.Hinge.CFrame
	local openDoor = TweenService:Create(self.Hinge, tweenInfo, {CFrame = hingeCFrame * CFrame.Angles(math.rad(90),0,0)})
	local closeDoor = TweenService:Create(self.Hinge, tweenInfo, {CFrame = hingeCFrame})
	self.Prompt.Triggered:Connect(function()
		self.Prompt.Enabled = false
		if self.Open == false then
			self.Frame.Open:Play()
			openDoor:Play()
			openDoor.Completed:Wait()
			self.Prompt.ActionText = "Close"
			self.Open = true
		elseif self.Open == true then
			closeDoor:Play()
			closeDoor.Completed:Wait()
			self.Frame.Close:Play()
			self.Prompt.ActionText = "Open"
			self.Open = false
		end
		self.Prompt.Enabled = true
	end)
end

return Door

Everything over this line could be used as a normal script for setting up/storing variables globally.
(local to that module)

local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.75, Enum.EasingStyle.Linear)

local Door = {}
Door.__index = Door

function Door.new(server, instance)
	local self = setmetatable({}, Door)
	self.Server = server
	self.Instance = instance
	self.Frame = instance.Frame
	self.Hinge = instance.Hinge
	self.Prompt = instance.Prompt
	self.Open = false

	return self
end

function Door:Init()
	local hingeCFrame = self.Hinge.CFrame
	local openDoor = TweenService:Create(self.Hinge, tweenInfo, {CFrame = hingeCFrame * CFrame.Angles(math.rad(90),0,0)})
	local closeDoor = TweenService:Create(self.Hinge, tweenInfo, {CFrame = hingeCFrame})
	self.Prompt.Triggered:Connect(function()
		self.Prompt.Enabled = false
		if self.Open == false then
			self.Frame.Open:Play()
			openDoor:Play()
			openDoor.Completed:Wait()
			self.Prompt.ActionText = "Close"
			self.Open = true
		elseif self.Open == true then
			closeDoor:Play()
			closeDoor.Completed:Wait()
			self.Frame.Close:Play()
			self.Prompt.ActionText = "Open"
			self.Open = false
		end
		self.Prompt.Enabled = true
	end)
end

return Door

I don’t have your door to test with so.. I could make one, but it may be different.
Posting your door would have been nice. (again, I’m lazy)

I’ve never used a Hinge on my doors, I just move the whole thing.

You don’t need to store it in the self. It is just unnecessary instances, you can store it in local variables, or remove them at all. For example:

local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.75, Enum.EasingStyle.Linear)

local Door = {}
Door.__index = Door

function Door.new(server, instance)
	local self = setmetatable({}, Door)

	self.Server = server
	self.Instance = instance
	self.Open = false

	return self
end

function Door:Init()
    -- i have removed these variables, so just using self.Instance.Hinge for example
	local hingeCFrame = self.Instance.Hinge.CFrame
	local openDoor = TweenService:Create(self.Instance.Hinge, tweenInfo, {CFrame = hingeCFrame * CFrame.Angles(math.rad(90),0,0)})
	local closeDoor = TweenService:Create(self.Instance.Hinge, tweenInfo, {CFrame = hingeCFrame})
	self.Instance.Prompt.Triggered:Connect(function()
		self.Instance.Prompt.Enabled = false
		if self.Open == false then
			self.Instance.Frame.Open:Play()
			openDoor:Play()
			openDoor.Completed:Wait()
			self.Instance.Prompt.ActionText = "Close"
			self.Open = true
		elseif self.Open == true then
			closeDoor:Play()
			closeDoor.Completed:Wait()
			self.Instance.Frame.Close:Play()
			self.Instance.Prompt.ActionText = "Open"
			self.Open = false
		end
		self.Instance.Prompt.Enabled = true
	end)
end

return Door

Also you can define types, for example:

--!strict
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.75, Enum.EasingStyle.Linear)

local Door = {}
Door.__index = Door

export type Door = {
    Instance: Instance & {
        Hinge: HingeConstraint,
        Frame: Frame,
        Prompt: ProximityPrompt,
    }, -- this will add autocomplete when you type self.Instance.
    Open: boolean,
    Server: any -- idk what it is :>
}

function Door.new(server, instance): Door
	local self: Door = setmetatable({}, Door) :: any

	self.Server = server
	self.Instance = instance
	self.Open = false

	return self
end

function Door.Init(self: Door) -- this will add type autocompletion on self
	local hingeCFrame = instance.Hinge.CFrame
	local openDoor = TweenService:Create(self.Instance.Hinge, tweenInfo, {CFrame = hingeCFrame * CFrame.Angles(math.rad(90),0,0)})
	local closeDoor = TweenService:Create(self.Instance.Hinge, tweenInfo, {CFrame = hingeCFrame})
	self.Prompt.Triggered:Connect(function()
		self.Instance.Prompt.Enabled = false
		if self.Open == false then
			self.Instance.Frame.Open:Play()
			openDoor:Play()
			openDoor.Completed:Wait()
			self.Instance.Prompt.ActionText = "Close"
			self.Open = true
		elseif self.Open == true then
			closeDoor:Play()
			closeDoor.Completed:Wait()
			self.Instance.Frame.Close:Play()
			self.Instance.Prompt.ActionText = "Open"
			self.Open = false
		end
		self.Instance.Prompt.Enabled = true
	end)
end

return Door

If you don’t know what is types, check this: Guide to Type-Checking with OOP

How do you just move the whole thing? I tried rotate the model around the pivot, but it just offsets the rotation.. Would be greatly appreciated

FordenRo That’s almost an exact copy of my script. Now I have even more hope that it will work out. :rofl:

Interesting stuff, thanks for this!

I just put a door down where it should go when opened, get the position and angle, delete it, and add that to my tween. Now that sounds barbaric compared to what you’re doing here. :rofl:

Sometimes I even put a transparent same object where I want my tween to go and tell it; go to where he is. Takes all the math out of in one shot.. :face_with_crossed_out_eyes: (rotation, angle and all).

I used that on a spaceship hatch as it could be anyplace.. Later I did figure out how to do it right.
Atm, I didn’t have time to bleed.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.