How can i improve this code

I am trying to create a tycoon merging game and what this code does is whenever you touch a button
and you have three dummies of the same type you merge themand get a better one, the only problem is that the code is very infficient.

local Button = script.Parent

local NormalAvatar = 0

local BlueAvatar = 0

local Avatars = {}
local NumberOfAvatars = 0

local debounce = false

local Index = 1 

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TweenService = game:GetService("TweenService")

Button.Touched:Connect(function(hit)
	if hit.Parent:WaitForChild("Humanoid") and debounce == false then
		debounce = true
		
		for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
			if v.Name == "NormalAvatar" then
				NormalAvatar += 1
			elseif v.Name == "BlueAvatar" then
				BlueAvatar += 1
			end
		end
		
		if NormalAvatar > 3 and NormalAvatar % 3 ~= 0 then
			repeat NormalAvatar -= 1 until NormalAvatar % 3 == 0
		end
		
		if BlueAvatar > 3 and BlueAvatar % 3 ~= 0 then
			repeat BlueAvatar -= 1 until BlueAvatar % 3 == 0
		end
		
		if NormalAvatar >= 3 then
			for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
				if NormalAvatar == 0 then continue end
				
				if v.Name == "NormalAvatar" then
					v:Destroy()
					
					NormalAvatar -= 1

					if i % 3 == 0 then
						local BlueAvatar = ReplicatedStorage.Avatars.BlueAvatar:Clone()
						BlueAvatar.Parent = game.Workspace.Tycoons.Tycoon1.Avatars
						
						for i ,v in pairs(BlueAvatar:GetDescendants()) do
							if v:IsA("Part") or v:IsA("MeshPart") then
								if v.Name ~= "HumanoidRootPart" then
									v.Transparency = 1

									local Tween = TweenService:Create(v,TweenInfo.new(.5,Enum.EasingStyle.Linear,Enum.EasingDirection.In),{Transparency = 0 })
									Tween:Play()

								end
							end
						end
						
					end
				end	
			end
			
			for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
				table.insert(Avatars , v)
			end

			NumberOfAvatars = #Avatars
			
			for i , v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
				
				if i == 1 then
					v.HumanoidRootPart.CFrame = CFrame.new(320, -28.712 , 117)
				elseif i % 5 == 0 and i == NumberOfAvatars then
					v.HumanoidRootPart.CFrame = CFrame.new(320, Avatars[Index].HumanoidRootPart.CFrame.Y + 9.288 , 117)

					Index = i
				elseif i < 5 then
					v.HumanoidRootPart.CFrame = CFrame.new(Avatars[i - 1].HumanoidRootPart.CFrame.X - 12, Avatars[1].HumanoidRootPart.CFrame.Y , 117)
				elseif i >= 5 and i ~= Index then
					v.HumanoidRootPart.CFrame = CFrame.new(Avatars[i - 1].HumanoidRootPart.CFrame.X - 12, Avatars[Index].HumanoidRootPart.CFrame.Y , 117)
				end	
			end		
		end
		

		for i = #Avatars , 1 , -1 do
			table.remove(Avatars,i)
		end
		
		
		NumberOfAvatars = 0
		NormalAvatar = 0
		BlueAvatar = 0
		
		wait(1)
		debounce = false
	end	
end)
  1. Move repetitive code to functions: You have repetitive code blocks that can be moved to functions to reduce the overall amount of code and make it easier to maintain. For example, you have similar code for counting the number of NormalAvatars and BlueAvatars. You can create a function that takes the Avatar type as a parameter and returns the count.
  2. Use variables wisely: You can use variables to store repeated values to avoid computing them multiple times. For example, you are computing the length of the Avatars table multiple times, which can be stored in a variable and used wherever needed.
  3. Optimize loops: You are using loops to iterate through the Avatars table multiple times, which can be improved. Instead, you can use a single loop to iterate through the table once and perform all necessary actions.
  4. Use local variables: Whenever possible, use local variables instead of global variables to reduce the amount of memory and improve the performance of your code.
  5. Use :IsA() instead of v.Name: Instead of checking the name of an object, you can use the :IsA() function to check if it is of a certain class. This can be useful if you decide to rename your objects in the future.
  6. Use continue keyword to skip iterations: Instead of using an if statement to check if a condition is met, you can use the continue keyword to skip iterations where the condition is not met.

This is an improved version of your code that I wrote! x

local Button = script.Parent
local debounce = false

local function countAvatarsOfType(type)
    local count = 0
    for _, v in pairs(game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()) do
        if v:IsA(type) then
            count += 1
        end
    end
    return count
end

local function mergeAvatars()
    local normalAvatarCount = countAvatarsOfType("Model")
    local blueAvatarCount = countAvatarsOfType("BlueAvatar")
    local avatars = game.Workspace.Tycoons.Tycoon1.Avatars:GetChildren()
    local avatarsLength = #avatars

    if normalAvatarCount >= 3 then
        for i, v in ipairs(avatars) do
            if normalAvatarCount == 0 then
                break
            end

            if v:IsA("Model") then
                v:Destroy()
                normalAvatarCount -= 1

                if i % 3 == 0 then
                    local blueAvatar = ReplicatedStorage.Avatars.BlueAvatar:Clone()
                    blueAvatar.Parent = game.Workspace.Tycoons.Tycoon1.Avatars

                    for _, part in ipairs(blueAvatar:GetDescendants()) do
                        if part:IsA("Part") or part:IsA("MeshPart") then
                            if part.Name ~= "HumanoidRootPart" then
                                part.Transparency = 1

                                local tween = TweenService:Create(part, TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), { Transparency = 0 })
                                tween:Play()
                            end
                        end
                    end
                end
            end
        end

        local index = 1
        for i, v in ipairs(avatars) do
            if i == 1 then
                v.HumanoidRootPart.CFrame = CFrame.new(320, -28.712, 117)
            elseif i % 5 == 0 and i == avatarsLength then
                v.HumanoidRootPart.CFrame = CFrame.new(

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.