I have been trying to think of ways to optimize this tweening script. Its purpose is to open and close a door that can have differing positions and rotations. All of the parts in the tweens have parts inside of them called 1 and 2 that represent the different CFrames and make it easier to move the door model. It is also controlled by a proximity prompt that changes its text. Also the “tweenInfo” instances are preset and don’t need to be changed.
function ChangeDoor(doorModel: Model, toggle : boolean)
--Set variable instances--
local Door : Folder = doorModel.Structure.Door
local Computer : Model = doorModel.Computer
local Prompt: Part = Computer.PromptPart
local Button: MeshPart = Computer.Button
local Doors12: Model = Door.Doors12
local Doors34: Model = Door.Doors34
local PositionValue
--Animate door instance--
if (toggle) then
Prompt.ProximityPrompt.ObjectText = "Door Opening"
PositionValue = 2
task.spawn(function()
task.wait(5)
Prompt.ProximityPrompt.ActionText = "Close Door"
end)
else
Prompt.ProximityPrompt.ObjectText = "Door Closing"
PositionValue = 1
task.spawn(function()
task.wait(5)
Prompt.ProximityPrompt.ActionText = "Open Door"
end)
end
local Doors12Tween = tS:Create(Doors12.PrimaryPart, DoorTweenInfo, {CFrame = Doors12:WaitForChild(PositionValue).CFrame})
local Doors34Tween = tS:Create(Doors34.PrimaryPart, DoorTweenInfo, {CFrame = Doors34:WaitForChild(PositionValue).CFrame})
Button.Color = Color3.fromRGB(255, 0, 0)
Prompt.ProximityPrompt.ActionText = "Cooldown"
Prompt.ProximityPrompt.HoldDuration = 0
Doors12Tween:Play()
Doors34Tween:Play()
Doors34Tween.Completed:Wait()
Button.Color = Color3.fromRGB(0, 255, 0)
Prompt.ProximityPrompt.ObjectText = ""
Prompt.ProximityPrompt.HoldDuration = 0.25
end
Any help on optimizing this would be appreciated, thanks!
You don’t really need to specify the object type if you’re not over riding the variable later in the script. Not ‘wrong’ by any means, just a tad slower.
That script is going to run really fast and you don’t optimization in there, there’s no loops or sorting or anything that would cause really concern for optimization.
I would however, recommend refactoring the code to be cleaner and more legible, there’s a decent amount of things you can do to improve the overall readability though.
Using those comments above, I was able to rewrite the code to the following:
function CreateDoorTween(door, PositionValue)
return tS:Create(door.PrimaryPart, DoorTweenInfo, {CFrame = door:WaitForChild(PositionValue).CFrame})
end
function ChangeDoor(doorModel: Model, toggle : boolean)
local PositionValue = toggle and 2 or 1
local Doors12Tween = CreateDoorTween(doorModel.Structure.Door.Doors12)
local Doors34Tween = CreateDoorTween(doorModel.Structure.Door.Doors34)
local Prompt: Part = doorModel.Computer.PromptPart
local Button: MeshPart = doorModel.Computer.Button
Prompt.ProximityPrompt.ObjectText = toggle and "Door Opening" or "Door Closing"
task.delay(5, function()
Prompt.ProximityPrompt.ActionText = toggle and "Close Door" or "Open Door"
end)
Button.Color = Color3.fromRGB(255, 0, 0)
Prompt.ProximityPrompt.ActionText = "Cooldown"
Prompt.ProximityPrompt.HoldDuration = 0
Doors12Tween:Play()
Doors34Tween:Play()
Doors34Tween.Completed:Wait()
Button.Color = Color3.fromRGB(0, 255, 0)
Prompt.ProximityPrompt.ObjectText = ""
Prompt.ProximityPrompt.HoldDuration = 0.25
end
By removing variables and if statements (by using ternary), you can more easily read the function which will help with code maintenance down the line (if ever needed). Also it cuts the function from 37 lines to 28
Ok thank you, I would’ve used ternary but I actually didn’t know one existed, and the use of a function is good. I like to keep most of the object type specifications just so I know what the variable is if I forget or for later modification to the script. But everything else looks way better. Thank you.
I am guessing the task.delay is just a combination of .spawn and .wait after looking at the docs, but if I am wrong please correct me.