Is it possible to optimize my scripts?

Sorry if this is in the wrong topic.

  1. Basically, I want to optimize my scripts as much as I can.

  2. I don’t see a way to optimize my scripts any further.

    Images of Locations and more
    Locations

    image


    Scripts
    Script in "FinalPart"
    local this_buttonGotHit = false
    local CompRoom = false
    local RoomFolder = script.Parent.Parent -- Choose where the current room folder should be.
    
    local door = RoomFolder.Door -- Choose
    
    local registeredPart = false
    if registeredPart == false then
     registeredPart = true
     wait(0.1)
     RoomFolder:SetAttribute("RequiredButtons",RoomFolder:GetAttribute("RequiredButtons") + 1)
    end
    
    
    local function TweenDoor()
     local duration = 1
     local style = 2
     local direction = 1
     local ts = game:GetService("TweenService")
     local tween1 = TweenInfo.new(duration,style,direction)
    
     local properties1 = {
     	Position = Vector3.new(door.Position.X,10,door.Position.Z),
     	Size = Vector3.new(11,0.05,1.75)
     }
    
    
    
     local create1 = ts:Create(door,tween1,properties1)
     
     create1:Play()
    end
    
    local function CheckToTween()
     if RoomFolder:GetAttribute("ButtonsHit") >= RoomFolder:GetAttribute("RequiredButtons") and 
    CompRoom == false then
     	CompRoom = true
     	wait(0.1)
     	TweenDoor()
     end
    end
    
    
    script.Parent.Touched:Connect(function(hit)
     if game.Players:GetPlayerFromCharacter(hit.Parent) then
     	if this_buttonGotHit == false then
     		if RoomFolder:GetAttribute("ButtonsHit") == 
    RoomFolder:GetAttribute("RequiredButtons") - 1 then
     		this_buttonGotHit = true
     		wait(0.1)
     		script.Sound:Play()
     		script.Parent.BrickColor = BrickColor.Red()
     		if RoomFolder:GetAttribute("ButtonsHit") < 
    RoomFolder:GetAttribute("RequiredButtons") then
     			RoomFolder:SetAttribute("ButtonsHit",RoomFolder:GetAttribute("ButtonsHit") + 1)
     				CheckToTween()
     			end	
     		end	
     	end
     end
    end)
    

    Script in every "Part"
    local this_buttonGotHit = false
    local CompRoom = false
    local RoomFolder = script.Parent.Parent -- Choose where the current room folder should be.
    
    local door = RoomFolder.Door -- Choose
    
    local registeredPart = false
    if registeredPart == false then
     registeredPart = true
     wait(0.1)
     RoomFolder:SetAttribute("RequiredButtons",RoomFolder:GetAttribute("RequiredButtons") + 1)
    end
    
    
    local function TweenDoor()
     local duration = 1
     local style = 2
     local direction = 1
     local ts = game:GetService("TweenService")
     local tween1 = TweenInfo.new(duration,style,direction)
    
     local properties1 = {
     	Position = Vector3.new(door.Position.X,10,door.Position.Z),
     	Size = Vector3.new(11,0.05,1.75)
     }
    
    
    
     local create1 = ts:Create(door,tween1,properties1)
     
     create1:Play()
    end
    
    
    script.Parent.Touched:Connect(function(hit)
     if game.Players:GetPlayerFromCharacter(hit.Parent) then
     	if RoomFolder:GetAttribute("ButtonsHit") >= RoomFolder:GetAttribute("RequiredButtons") 
    and CompRoom == false then
     		CompRoom = true
     		wait(0.1)
     		TweenDoor()
     	end
     	if this_buttonGotHit == false then
     		this_buttonGotHit = true
     		wait(0.1)
     		script.Sound:Play()
     		script.Parent.BrickColor = BrickColor.Red()
     		if RoomFolder:GetAttribute("ButtonsHit") < 
    RoomFolder:GetAttribute("RequiredButtons") then
     			RoomFolder:SetAttribute("ButtonsHit",RoomFolder:GetAttribute("ButtonsHit") + 1)
     		end	
     	end
     end
    end)
    

    Script in "Room1"
    local RoomFolder = script.Parent
    local FinalButton = RoomFolder.FinalPart
    
    RoomFolder:GetAttributeChangedSignal("ButtonsHit"):Connect(function()
     if RoomFolder:GetAttribute("ButtonsHit") == RoomFolder:GetAttribute("RequiredButtons") - 1 then
     	FinalButton.Color = Color3.fromRGB(0,255,0)
     end
    end)
    

    Thank you for putting the time into reading this and helping out!

Is your game running too slow? If you aren’t having significant performance issues, then you shouldn’t be too worried (unless you plan on expanding your project a lot).

Instead of having a script for each part, it would be better to handle all of them in one script. They can still have their own independent variables, but you would only use one controlling script.

Some other feedback on the first/second script (they seem nearly identical):

Why? All of these lines except two are pointless. Could be changed to:

local registeredPart = true
RoomFolder:SetAttribute("RequiredButtons",RoomFolder:GetAttribute("RequiredButtons") + 1)

Random wait() with no purpose can be removed.

If this is happening a lot I would make players into a variable local players = game:GetService("Players").

Since there are no elseifs/else added to these ifs, you can just connect these through and:

 if game.Players:GetPlayerFromCharacter(hit.Parent)
  and not this_buttonGotHit
  and RoomFolder:GetAttribute("ButtonsHit") == RoomFolder:GetAttribute("RequiredButtons") - 1 then

In addition, it looks like you don’t actually reset this_buttonGotHit back to false anywhere. If that’s the case, that variable doesn’t have any use. You can just disconnect the event after you confirm it’s valid:

local connection
connection = script.Parent.Touched:Connect(function(hit)
...
	-- instead of this_buttonGotHit = true here:
	connection:Disconnect()

You don’t do anything with this CompRoom (debounce?) variable either, so you can just get rid of it.

1 Like