Optimizing a door script

Hello everyone, im a bit stuck on this and I figured the best would be to ask here before I start with the building.
Currently im building an underground facility, SCP-style but mixed with black mesa.

Now, I have most if not all of the game figured except one part: Script optimization
the old map I have can be laggy some times and unlike it I want to make the new one with performance in mind.

So the important question: How can I optimize a script meant to be repeated various time? (as in every door has its own script)
also as a side question: do functions affect performance by a lot?

local prox = script.Parent.Puerta.TouchPart.ProximityPrompt
local pivot = script.Parent.Puerta.Pivot
local TweenService = game:GetService("TweenService")
toggle = true
----
local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear ,Enum.EasingDirection.Out,0,false,0)
local tweenOpen = TweenService:Create(pivot, tweenInfo, {CFrame = pivot.CFrame * CFrame.Angles(math.rad(90), 0, 0)})
local tweenClose = TweenService:Create(pivot, tweenInfo, {CFrame = pivot.CFrame * CFrame.Angles(math.rad(0),0, 0)})
----
prox.Triggered:Connect(function(player)
	if toggle == true then
		pivot.Parent.PartSound.Puerta:Play()
		tweenOpen:play()
		toggle = false
	elseif toggle == false then
		pivot.Parent.PartSound.Puerta:Play()
		tweenClose:play()
		toggle = true
	end
end)
--

This script here is a small single door.

local pivot1 = script.Parent.PuertaDerecha.Pivot
local pivot2 = script.Parent.PuertaIzquierda.Pivot
local TweenService = game:GetService("TweenService")
local hit1 = script.Parent.Hitbox1
local hit2 = script.Parent.Hitbox2
local p1 = script.Parent.Lector1.Pantalla
local p2 = script.Parent.Lector2.Pantalla
local soundp = script.Parent.Soundpart
toggle = true
------
local tweenInfo = TweenInfo.new(2,Enum.EasingStyle.Quad ,Enum.EasingDirection.Out,0,false,0)
local tweenOpen1 = TweenService:Create(pivot1, tweenInfo, {CFrame = pivot1.CFrame * CFrame.Angles(math.rad(90), 0, 0)})
local tweenClose1 = TweenService:Create(pivot1, tweenInfo, {CFrame = pivot1.CFrame * CFrame.Angles(math.rad(0),0, 0)})
local tweenOpen2 = TweenService:Create(pivot2, tweenInfo, {CFrame = pivot2.CFrame * CFrame.Angles(math.rad(-90), 0, 0)})
local tweenClose2 = TweenService:Create(pivot2, tweenInfo, {CFrame = pivot2.CFrame * CFrame.Angles(math.rad(0),0, 0)})
------
function OpenDoors()
	tweenOpen2:Play()
	tweenOpen1:Play()
	p1.BrickColor = BrickColor.new("Bright green")
	p2.BrickColor = BrickColor.new("Bright green")
	hit1.ProximityPrompt.ActionText = "Cerrar"
	hit2.ProximityPrompt.ActionText = "Cerrar"
	soundp.Puerta:Play()
end
--
function CloseDoors()
	tweenClose2:Play()
	tweenClose1:Play()
	p1.BrickColor = BrickColor.new("Bright red")
	p2.BrickColor = BrickColor.new("Bright red")
	hit1.ProximityPrompt.ActionText = "Abrir"
	hit2.ProximityPrompt.ActionText = "Abrir"
	soundp.Puerta:Play()
end
--
function CheckOpening()
	if toggle == true then
		OpenDoors()
		soundp.Beep:Play() 
		toggle = false
	elseif toggle == false then
		CloseDoors()
		toggle = true
	end
end

hit1.ProximityPrompt.Triggered:Connect(CheckOpening)
hit2.ProximityPrompt.Triggered:Connect(CheckOpening)

And this one here is a double door but apart for the tween part it should function the same. This one is longer since I wanted to try out functions. Originally I used a single hitbox to display the proximityPrompt and for sounds as the door was small and was meant to have a handle, but with the bigger door I experimented and found this way to have as many buttons as I wanted without repeating the code (Like I had done with a keycard double door before)

But now I wonder: Is this really worth it? Is there any better way to get this done?

Thanks in advance for any answers

I’ve written a guide on optimizing here if you want to check it out.


In terms of optimization, I don’t really see much you can actually do. One huge thing is that to attempt to reduce instances, if possible you want to somehow label all the doors and use one script to handle it all, create instances when it’s needed instead of it having it sit around. In terms of Tweening and Animating, it’s best to have visuals done on the client unless you need it replicated across all the other Clients. This post would have been better to be posted in Code Review section rather than Scripting Support. Hopefully that answered you some questions. An example of handling everything in one place:

local Folder = ... -- Holds all doors
local Children = Folder:GetChildren();
for i = 1, #Children do
    local Child = Children[i] -- Child would be the door, you can access anything else of the Child by doing Child.Whatever
    -- Connect events, do stuff to each Door
end
2 Likes