Review of the optimization of my code

  • How could I possibly optimize the code ?
function Reroll:ExtrasReroll(player, name, description, color)
		--// Player Information
		local playerGui = player:FindFirstChild("PlayerGui") :: PlayerGui
		--// Gui Information
		local rerollButton = playerGui.BottomGui.BottomFrame.RerollButton :: TextButton
		local blackFrame = playerGui.CenterGui.BlackFrame :: Frame
		local nameText = blackFrame.NameText :: TextLabel
		local descriptionText = blackFrame.DescriptionText :: TextLabel
		--// Code:
		for _, text in blackFrame:GetChildren() do
			if text:IsA("TextLabel") then
				nameText.Text = name
				descriptionText.Text = description
				text.TextColor3 = color
				rerollButton.Visible = false
				TweenService:Create(blackFrame, TweenInfo.new(0.35), {BackgroundTransparency = 0}):Play()
				TweenService:Create(text, TweenInfo.new(0.25), {TextTransparency = 0}):Play()
				wait(3)
				TweenService:Create(blackFrame, TweenInfo.new(0.25), {BackgroundTransparency = 1}):Play()
				TweenService:Create(text, TweenInfo.new(0.25), {TextTransparency = 1}):Play()
				rerollButton.Visible = true
			end
		end
	end

atp wait() is just deprecated because of how bad it is

use task.wait()

but the rest looks fine

(op: Wait() vs task.wait())

1 Like

Ok, thank you very much for your reply

1 Like

This is probably just a basic tip but maybe create the tweens as variables before the loop instead of doing the old TweenService:Create():Play()

Edit: my mistake i didn’t realize that text was part of the for loop

1 Like

Yes text was part of the for loop

  1. Put --!strict at the top of your script and start fixing all the warnings.

  2. Take advantage of type casting. (player: Player, name: string, description: string, color: Color3)

  3. Favor task.wait() over wait().

  4. Additionally, you’re using those four tweens over and over again. Presumably. I suggest creating all four of them as local variables outside of the script and just calling :Play() on the variable when needed.

1 Like