Optimizing and fixing my whole script for hyperspace

My script is pretty much fully broken, everything is mostly placeholders right now

local textButton = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton
local warp1 = game.Workspace.Group1
local warp2 = game.Workspace.Group2
local warpTunnel = game.Workspace.tunnel
local targetFOV = 200
local Frame = game.Players.LocalPlayer.PlayerGui.White.Frame
local BackgroundTransparency = Frame.BackgroundTransparency

local easingDuration = 4
local easingStyle = Enum.EasingStyle.Sine
local easingDirection = Enum.EasingDirection.Out

textButton.MouseButton1Click:Connect(function()
	for I, V in pairs (warp1:GetDescendants()) do
		
		if V:IsA("ParticleEmitter") then
			V.Enabled = true
			wait()
			
			for I, V in pairs (warp2:GetDescendants()) do
				
				if V:IsA("ParticleEmitter") then
					V.Enabled = true
					
					task.wait(2)
					
					local tween = game.TweenService:Create(workspace.CurrentCamera, TweenInfo.new(easingDuration, easingStyle, easingDirection), {
						FieldOfView = targetFOV
					})
					
					tween:Play()
					
					task.wait(3)
					
					for i = 1,100 do
						Frame.BackgroundTransparency = Frame.BackgroundTransparency - 0.01
					end
					
					for I, V in pairs(warpTunnel:GetDescendants()) do
						if V:IsA("Beam") then
							V.Enabled = true
							for i = 1,100 do
								Frame.BackgroundTransparency = Frame.BackgroundTransparency + 0.01
							end
						end
					end
				end
			end
		end
	end
end)

So, only two random particles will activate

Should be more like that but that version of the script would run without me pressing the button

Also the white GUI doesn’t fade in or out or even appear at all and I always get an error of textButton not being apart of ScreenGui

Use GetService(). Just indexing for the service is a bad coding practice because the service is not guaranteed to be loaded at the time of use.

Firstly, pairs() is no longer necessary because of generalized iteration, which is faster than pairs.

Secondly, you really need a better way of organization. Iterating through all descendants is a really bad idea for performance. Consider using tags (from CollectionService or from the Instance method), or a table that already stores all of the instances.

Why? You used task.wait() later in the code, so what is the meaning of this?

It’s NEVER a good idea to chain for-loops like this. You are exponentially increasing the processing time.

Once again, use GetService and cache the service in a variable. Redundantly accessing the Roblox hierarchy is bad because of all the sandboxing and metatable hooks involved. It would be miles better to access it once and then reuse it.

TweenInfos can be reused. Make just one, store it as a variable at the top of the script, and use that variable. Right now you are making a brand new one for every tween.

Use compound assignment x += y instead of x = x + y. It will only evaluate the assignee once, which is better for performance.

It’s not even yielding anywhere bro :skull: you forgot to add task.wait()