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