How do I make my code, smaller more effects and more readable?

The code already works. But the Attack Sequence code near the bottom has some patterns I just don’t know how to shorten it.


–Made by Its4Realzies
return function()
–Shortcuts
local sP = script.Parent
local pLR = game.Players.LocalPlayer
local cPLR = pLR.Character

local outer = sP.Outer
local center = sP.Center

local partGoal = {}
local partColors = {
    Color3.fromRGB(190, 0, 0),
    Color3.fromRGB(227, 163, 33),
    Color3.fromRGB(0, 166, 33),
    Color3.fromRGB(0, 133, 200)
}

--Very handy tween function
local function doTween(tweenedPart, goal, tweenDuration, easingStyle, easingDirection, tweenDelay)
    local tweenService = game:GetService("TweenService")
    local tweeningInformation =
        TweenInfo.new(
        tweenDuration, --Length
        easingStyle, --Easing style
        easingDirection, --Direction
        0, --Number of times tween will repeat
        false, --Should tween repeat
        tweenDelay --Delay between tweens
    )
    local tween = tweenService:Create(tweenedPart, tweeningInformation, goal)
    tween:Play()
end

--Adds 0.5 and then cuts off the decimal
local function round(num, mult)
    return math.floor(num / mult + 0.5) * mult
end

--Gravitys
local gravity = Vector3.new(0, -game.Workspace.Gravity, 0)
local xGravityP = Vector3.new(game.Workspace.Gravity, 0, 0)
local xGravityN = Vector3.new(-game.Workspace.Gravity, 0, 0)
local zGravityP = Vector3.new(0, 0, game.Workspace.Gravity)
local ZGravityN = Vector3.new(0, 0, -game.Workspace.Gravity)
local gravitys = {xGravityP, xGravityN, zGravityP, ZGravityN}

--Calculates projectiles trajectory
local function doHop(targetPosition, part, partGravity)
    local startPosition = part.Position
    local Velocity = (targetPosition - startPosition - 0.5 * partGravity)

    part.Velocity = Velocity
end

--Creates bullet for shooting attacks
local function createBullet(bulletDamage, deletionDelay)
    --Creates bullet
    local bullet = Instance.new("Part", sP.BulletStorage)
    bullet.Material = "Neon"
    bullet.Transparency = 0
    bullet.Name = "Bullet"
    bullet.CanCollide = false
    bullet.Shape = "Ball"
    bullet.Size = Vector3.new(outer.Size.X / 2, outer.Size.Y / 2, outer.Size.Z / 2)
    bullet.Color = outer.Color
    bullet.CFrame = outer.CFrame

    game.Debris:AddItem(bullet, deletionDelay)

    bullet.Touched:Connect(
        function(t)
            if t.CanCollide and t ~= sP and t.Parent ~= sP then
                bullet:Destroy()
                local h = t.Parent:FindFirstChild "Humanoid"
                if h and t:IsDescendantOf(pLR.Character) then
                    game.ReplicatedStorage.DamageEvent:FireServer(bulletDamage)
                end
            end
        end
    )

    return bullet
end

--Basicly sidways gravity  mortar turrets
local function doGravityScatter(bulletDamage)
    for i = 1, 4, 1 do
        local currentBullet = createBullet()

        local currentGravity = gravitys[i]
        --Adds bodyForce
        local bodyForce = Instance.new("BodyForce", currentBullet)
        bodyForce.Force =
            Vector3.new(
            currentBullet:GetMass() * currentGravity.X,
            currentBullet:GetMass() * game.Workspace.Gravity,
            currentBullet:GetMass() * currentGravity.Z
        )

        --Adds velocity
        doHop(
            Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z),
            currentBullet,
            currentGravity
        )
    end
end

--Infinitely scalable scatter function
local function doScatter(bulletCount, bulletSpeed, bulletDamage, deletionDelay)
    for i = 1, bulletCount, 1 do
        local currentBullet = createBullet(bulletDamage, deletionDelay)
        local newBodyVelocity = Instance.new("BodyVelocity", currentBullet)
        newBodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
        newBodyVelocity.P = 1250
        newBodyVelocity.Velocity =
            Vector3.new(
            bulletSpeed * (math.sin(2 * math.pi * i / bulletCount)),
            0,
            bulletSpeed * (math.cos(2 * math.pi * i / bulletCount))
        )
    end
end

local function doWebDance(bulletCount, bulletDamage)
    function getRandomPosition()
        local randomPosition = nil
        repeat
            randomPosition =
                Vector3.new(
                outer.Position.X + math.random(-50, 50),
                outer.Position.Y,
                outer.Position.Z + math.random(-50, 50)
            )
        until (randomPosition - outer.Position).Magnitude > 5 and
            math.abs(randomPosition.X) + math.abs(randomPosition.Z) < 70
        return randomPosition
    end

    for i = 1, bulletCount, 1 do
        local currentBullet = createBullet(bulletDamage, 100)

        --Creates bodyPosition
        local bodyPosition = Instance.new("BodyPosition", currentBullet)
        bodyPosition.Position = getRandomPosition()
        bodyPosition.D = 300
        bodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
        --Vector3.new(math.huge, math.huge, math.huge)
        bodyPosition.P = 500
    end

    wait(1.5)
    --Moves bullets
    local bulletsMoved = 0
    repeat
        wait(0.5)
        for i, child in ipairs(sP.BulletStorage:GetChildren()) do
            if child.ClassName == "Part" then
                if bulletsMoved == 0 and child:FindFirstChild("BodyPosition") then
                    child.BodyPosition:Destroy()

                    local newAttachment0 = Instance.new("Attachment", child)
                    newAttachment0.WorldCFrame =
                        CFrame.new(child.Position, sP.Spinner.Position) * CFrame.Angles(0, math.rad(90), 0)

                    local newAttachment1 = Instance.new("Attachment", sP.BulletSpinner)
                    newAttachment1.WorldCFrame =
                        CFrame.new(sP.Spinner.Position, child.Position) * CFrame.Angles(0, math.rad(90), 0)

                    local newPrismatic = Instance.new("PrismaticConstraint", child)
                    newPrismatic.Attachment0 = newAttachment1
                    newPrismatic.Attachment1 = newAttachment0
                    newPrismatic.LimitsEnabled = true
                    newPrismatic.UpperLimit = 50

                    newPrismatic.ActuatorType = "Servo"
                    newPrismatic.ServoMaxForce = 10000
                    newPrismatic.Speed = 5
                end

                if bulletsMoved >= 4 then
                    child:FindFirstChild("PrismaticConstraint").Speed = 100
                    child:FindFirstChild("PrismaticConstraint").TargetPosition = 3
                else
                    child:FindFirstChild("PrismaticConstraint").TargetPosition =
                        (sP.BulletSpinner.Position - child.Position).Magnitude + math.random(-20, 20)
                end
            end
        end

        print("bulletsMoved")
        bulletsMoved = bulletsMoved + 1
    until bulletsMoved >= 5
end

--Its something...
local function doMermog(bulletDamage)
    function shootMermogBullet(bullet)
        bullet.Anchored = true
        local t = 0 --The percentage of the arch 0=0% 1=100%

        local bP = bullet.Position --Start/Beginning position
        local tP = Vector3.new(cPLR.Torso.Position.X, outer.Position.Y, cPLR.Torso.Position.Z) --Target position

        local mP = Vector3.new((bP.X + tP.X) / 2, outer.Position.Y, (bP.Z + tP.Z) / 2) --Midpoint to help find control position

        local controlDistance = (tP - bP).Magnitude --change to distance formula later
        print("Control distance is " .. controlDistance)

        local x1 = bP.X
        local z1 = bP.Z

        local x2 = tP.X
        local z2 = tP.Z
        print("x1 is " .. x1)
        print("z1 is " .. z1)

        print("x2 is " .. x2)
        print("z2 is " .. z2)

        --local pathSlope = z2 - z1 / x2 - x1
        local pathSlope = (x2 - x1) / (z2 - z1)

        local perpSlope = -1 / pathSlope

        print("pathSlope is " .. pathSlope)
        print("perpSlope is " .. perpSlope)

        --local newX = mP.X + (controlDistance / 2) / math.sqrt(1 + perpSlope * perpSlope)
        --local newZ = mP.Z + (controlDistance / 2) / math.sqrt(1 + perpSlope * perpSlope)
        --local newX = perpSlope * controlDistance / 4 + mP.X
        --local newZ = (1/perpSlope) * controlDistance / 4 + mP.Z
        local newX = (perpSlope / (perpSlope + 1 / perpSlope)) * controlDistance / 2 + mP.X
        local newZ = ((1 / perpSlope) / (perpSlope + 1 / perpSlope)) * controlDistance / 2 + mP.Z

        print("newX is " .. newX)
        print("newZ is " .. newZ)

        print("mPX is " .. mP.X)
        print("mPZ is " .. mP.Z)

        local cP = Vector3.new(newX, outer.Position.Y, newZ)
        --
        --[[
		local visual = Instance.new("Part", workspace)
		visual.Anchored = true
		visual.Size = Vector3.new(2, 2, 2)
		visual.Shape = 'Ball'
		visual.BrickColor = BrickColor.new("Black")
		visual.Position = cP
			
		local visual2 = Instance.new("Part", workspace)
		visual2.Anchored = true
		visual2.Size = Vector3.new(2,2,2)
		visual2.Shape = 'Ball'
		visual2.BrickColor = BrickColor.new("Burlap")
		visual2.Position = mP
		]] local stepped
        stepped =
            game:service "RunService".Stepped:connect(
            function()
                if bullet ~= nil then --and t < 10 then
                    local pFinalX = math.pow(1 - t, 2) * bP.X + (1 - t) * 2 * t * cP.X + t * t * tP.X
                    local pFinalZ = math.pow(1 - t, 2) * bP.Z + (1 - t) * 2 * t * cP.Z + t * t * tP.Z
                    t = t + 0.025
                    bullet.CFrame = CFrame.new(pFinalX, bP.Y, pFinalZ)
                else
                    print("End reached")
                    bullet:Destroy()
                    stepped:Disconnect()
                end
            end
        )
    end

    local mBulletsShot = 0
    for i, child in ipairs(sP.BulletStorage:GetChildren()) do
        if child.ClassName == "Part" then
            if mBulletsShot % 4 == 0 then
                wait(0.5)
            end

            --Create reference to a BindableEvent
            local triggerBullet = Instance.new("BindableEvent")
            triggerBullet.Event:Connect(shootMermogBullet(child))
            triggerBullet:Fire()

            mBulletsShot = mBulletsShot + 1
        end
    end
end

--This is the fun part
local function attackSequence()
    --Activation
    wait(1)
    partGoal.Size = Vector3.new(6, 6, 6)
    partGoal.Color = partColors[1]
    doTween(outer, partGoal, 1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out, 1)
    wait(1)
    doScatter(50, 30, 5, 2) --Hop attack 1
    --

    --[[]] wait(1)
    doScatter(25, 15, 10, 9)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 7)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 5)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 3)
    doHop(center.Position, outer, gravity)

    wait(1.5)
    outer.Color = partColors[4]

    --Bullet reverse
    for i, child in ipairs(sP.BulletStorage:GetChildren()) do
        if child.ClassName == "Part" and child:FindFirstChild("BodyVelocity") then
            child:FindFirstChild("BodyVelocity").Velocity = child:FindFirstChild("BodyVelocity").Velocity * -1
            child.Color = partColors[4]
        end
    end

    --Gravity attack 1
    wait(2.5)
    doGravityScatter(outer, 30)
    wait(1)
    doGravityScatter(outer, 30)
    wait(0.5)
    doGravityScatter(outer, 30)
    wait(0.5)
    doGravityScatter(outer, 30)
    wait(0.25)
    doGravityScatter(outer, 30)
    wait(0.25)
    doGravityScatter(outer, 30)
    wait(0.125)
    doGravityScatter(outer, 30)
    wait(0.125)
    doGravityScatter(outer, 30)

    --Hop attack 2
    wait(1)
    outer.Color = partColors[1]
    doScatter(25, 15, 10, 9)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 7)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 5)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 3)
    doHop(center.Position, outer, gravity)

    wait(1.5)
    outer.Color = partColors[4]

    --Bullet reverse
    for i, child in ipairs(sP.BulletStorage:GetChildren()) do
        if child.ClassName == "Part" and child:FindFirstChild("BodyVelocity") then
            child:FindFirstChild("BodyVelocity").Velocity = child:FindFirstChild("BodyVelocity").Velocity * -1
            child.Color = partColors[4]
        end
    end

    --Gravity attack 2
    wait(2.5)
    doGravityScatter(outer, 30)
    wait(1)
    doGravityScatter(outer, 30)
    wait(0.5)
    doGravityScatter(outer, 30)
    wait(0.5)
    doGravityScatter(outer, 30)
    wait(0.25)
    doGravityScatter(outer, 30)
    wait(0.25)
    doGravityScatter(outer, 30)
    wait(0.125)
    doGravityScatter(outer, 30)
    wait(0.125)
    doGravityScatter(outer, 30)

    --Hop attack 3
    wait(2)
    outer.Color = partColors[1]
    doScatter(25, 15, 10, 15)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 13)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 11)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 9)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y + 3.25, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 7)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y + 3.25, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 5)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y + 3.25, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 3)
    doHop(center.Position, outer, gravity)

    wait(1.5)
    outer.Color = partColors[4]

    --Bullet reverse
    for i, child in ipairs(sP.BulletStorage:GetChildren()) do
        if child.ClassName == "Part" and child:FindFirstChild("BodyVelocity") then
            child:FindFirstChild("BodyVelocity").Velocity = child:FindFirstChild("BodyVelocity").Velocity * -1
            child.Color = partColors[4]
        end
    end

    --Hop attack 4
    wait(2)
    outer.Color = partColors[1]
    doScatter(25, 15, 10, 9)
    doScatter(24, 30, 10, 9)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 8)
    doScatter(24, 30, 10, 8)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 7)
    doScatter(24, 30, 10, 7)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    doScatter(25, 15, 10, 6)
    doScatter(24, 30, 10, 6)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 5)
    doScatter(24, 30, 10, 5)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 4)
    doScatter(24, 30, 10, 4)
    doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y, cPLR.Torso.Position.Z), outer, gravity)
    wait(1)
    doScatter(25, 15, 10, 3)
    doHop(center.Position, outer, gravity)

    wait(2)
    --Bullet hop
    for i, child in ipairs(sP.BulletStorage:GetChildren()) do
        if child.ClassName == "Part" and child:FindFirstChild("BodyVelocity") then
            child:FindFirstChild("BodyVelocity"):Destroy()
            child.Color = partColors[3]
            doHop(outer.Position, child, gravity)
        end
    end
    --
    --[[]] wait(1)
    outer.Color = partColors[3]
    wait(0.5)
    doWebDance(100, 10)

    wait(2)
    outer.Color = partColors[2]
    --Bullet color
    for i, child in ipairs(sP.BulletStorage:GetChildren()) do
        if child.ClassName == "Part" then
            child.Anchored = true
            child.Color = partColors[2]
        end
    end

    doMermog(10)
end

wait(1)
attackSequence()

end


I shortered two pieced of your code. I didn’t test it (so you should test it) and it seems to be correct:

--Gravity attack 2 (shorter version)
for i, v in pairs({2.5, 1, 0.5, 0.5, 0.25, 0.25, 0.125, 0.125}) do
	wait(v)
	doGravityScatter(outer, 30)
end

--Hop attack 3 (shorter version)
wait(2)
outer.Color = partColors[1]
local scatterLastNum = 15
for i, v in pairs({0, 3.25}) do
	for i = 1, 3 do
		doScatter(25, 15, 10, scatterLastNum)
		if v ~= 3.25 and i ~= 3 then
			doHop(Vector3.new(cPLR.Torso.Position.X, center.Position.Y + v, cPLR.Torso.Position.Z), outer, gravity)
			wait(1)
		else
			doHop(center.Position, outer, gravity)
		end
		scatterLastNum -= 2
	end
end
1 Like

Ok so the v in pairs waits in that list so it works the same as that big block? Woah thanks I would never think of that!

So I need to find a pattern and use tricks like that to shorten?

Im 99% sure that Gravity attack 2 (shorter version) works like the orginal version, but it’s shorter.

1 Like

Yes and there are other ways of shortering you code. A hint:
local vec = Vector3.new; print(vec(0, 16, 0), typeof(vec(0, 16, 0)))

1 Like

How does that work and whats it for?

You can use that to type “vec(x, y , z)” instead of “Vector3.new(x, y , z)” and it’s shorter.

1 Like