Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to optimize cloning in new roblox update?

Asked by 3 years ago

So basically everything has been going smooth until the new roblox update and now my barrage effect clones make the game insanely laggy.

This is the code any help on optimizing it is greatly appreciated I know this might sound lazy but it would be great if you could optimize it for me and I can review the code. I have 0 idea how to optimize and this is my last hope.

local EffectModule = require(game.ServerStorage:WaitForChild("HitEffectModule"))
local event = game:GetService("ReplicatedStorage"):WaitForChild("TheWorld").Barrage -- this is the event which fires from client to server
local TweenService = game:GetService("TweenService") -- this is a very useful function for extra effects
local Debris = game:GetService("Debris")
local Damage = 1.8
local Punched = game.Workspace.Sounds.Punched



--u can get ur own one im just using a free one as an example
local function Detect(Character)
    local Hitbox = script.Hitbox:Clone()
    Hitbox.Parent = workspace
    Hitbox.CFrame = Character.PrimaryPart.CFrame * CFrame.new(0,0,Hitbox.Size.Z/2 * -1)
    Debris:AddItem(Hitbox,.3)
    local Connection 

    Connection = Hitbox.Touched:Connect(function(hitted)
        if hitted.Parent:FindFirstChild("Humanoid") and not hitted:IsDescendantOf(Character) then
            local Enemy = hitted.Parent
            Enemy.Humanoid:TakeDamage(Damage)

            Connection:Disconnect()
            Hitbox:Destroy()
            local bv = Instance.new("BodyVelocity")
            bv.MaxForce = Vector3.new(1e6,1e6,1e6)
            bv.Velocity = Character.PrimaryPart.CFrame.LookVector * 10
            Debris:AddItem(bv,.2)
            bv.Parent = Enemy.PrimaryPart

            EffectModule.Gore2(Enemy.UpperTorso)
            Enemy.Humanoid:LoadAnimation(script.HitAnim):Play()
            Punched:Play()
            Enemy.Humanoid.WalkSpeed = 3
            wait(1)
            Enemy.Humanoid.WalkSpeed = 16
        end
    end)






end













local function CreateArm(Character)
    local Stand = game:GetService("ServerStorage").StandModel.TheWorld
    local StartXR = math.random(150,350)/100
    Detect(Character)
    local StartXL = math.random(150,350)/100 * -1
    local StartYR = math.random(-150,250)/100
    local StartYL = math.random(-150,250)/100
    local LeftArmModel = Instance.new("Model")
    LeftArmModel.Name = "LeftArm"
    LeftArmModel.Parent = game.Workspace
    Debris:AddItem(LeftArmModel,.3)
    local LLA = Stand:FindFirstChild("LeftLowerArm"):Clone()
    LLA.Parent = LeftArmModel
    local LUA = Stand:FindFirstChild("LeftUpperArm"):Clone()
    LUA.Parent = LeftArmModel
    local LH = Stand:FindFirstChild("LeftHand"):Clone()
    LH.Parent = LeftArmModel
    local weld1 = Instance.new("WeldConstraint")
    weld1.Part0 = LH
    weld1.Part1 = LLA
    weld1.Parent = LH 
    local weld2 = Instance.new("WeldConstraint")
    weld2.Part0 = LUA
    weld2.Part1 = LLA
    weld2.Parent = LUA
    for i ,  v  in pairs(LeftArmModel:GetDescendants()) do
        if v:IsA("Texture") or v:IsA("BasePart") or v:IsA("Decal") then
            v.Transparency = .2
            TweenService:Create(v,TweenInfo.new(.3),{Transparency = .75}):Play()
        end
        if v:IsA("Motor6D") then
            v:Destroy()
        end
    end
    LeftArmModel.PrimaryPart = LLA
    LeftArmModel.PrimaryPart.Anchored = true

    LeftArmModel.PrimaryPart.CFrame = CFrame.new(Character.PrimaryPart.CFrame * CFrame.new(StartXL,StartYL,-1.5).p,Character.PrimaryPart.CFrame * CFrame.new(StartXL * .4,StartYL * .4,-8).p) * CFrame.Angles(math.rad(90),0,0)
    TweenService:Create(LeftArmModel.PrimaryPart,TweenInfo.new(.3),{CFrame = LeftArmModel.PrimaryPart.CFrame * CFrame.new(0,-5,0)}):Play()
    local RightArmModel = Instance.new("Model")
    RightArmModel.Name = "LeftArm"
    RightArmModel.Parent = game.Workspace
    Debris:AddItem(RightArmModel,.3)
    local RLA = Stand:FindFirstChild("RightLowerArm"):Clone()
    RLA.Parent = RightArmModel
    local RUA = Stand:FindFirstChild("RightUpperArm"):Clone()
    RUA.Parent = RightArmModel
    local RH = Stand:FindFirstChild("RightHand"):Clone()
    RH.Parent = RightArmModel
    local weld1 = Instance.new("WeldConstraint")
    weld1.Part0 = RH
    weld1.Part1 = RLA
    weld1.Parent = RH 
    local weld2 = Instance.new("WeldConstraint")
    weld2.Part0 = RUA
    weld2.Part1 = RLA
    weld2.Parent = RUA
    for i ,  v  in pairs(RightArmModel:GetDescendants()) do
        if v:IsA("Texture") or v:IsA("BasePart") or v:IsA("Decal") then
            v.Transparency = .2
            TweenService:Create(v,TweenInfo.new(.3),{Transparency = .75}):Play()
        end
        if v:IsA("Motor6D") then
            v:Destroy()
        end
    end
    RightArmModel.PrimaryPart = RLA
    RightArmModel.PrimaryPart.Anchored = true

    RightArmModel.PrimaryPart.CFrame = CFrame.new(Character.PrimaryPart.CFrame * CFrame.new(StartXR,StartYR,-1.5).p,Character.PrimaryPart.CFrame * CFrame.new(StartXR * .4,StartYR * .4,-8).p) * CFrame.Angles(math.rad(90),0,0)
    TweenService:Create(RightArmModel.PrimaryPart,TweenInfo.new(.3),{CFrame = RightArmModel.PrimaryPart.CFrame * CFrame.new(0,-5,0)}):Play()




end



event.OnServerEvent:Connect(function(Player,hold)
    local Character = Player.Character
    local Stand = Character:FindFirstChild(Character.Name)
    if Stand then
        if not Character:FindFirstChild("Barraging") then

            if hold == true and Character.UsingMove.Value == false then
                local barraging = Instance.new("BoolValue")
                barraging.Name = "Barraging"
                barraging.Parent = Character
                Character.UsingMove.Value = true
                Character.PrimaryPart.StandPosition.Position = Vector3.new(0,-1,-2)
                local Anim = Stand.AnimationController:LoadAnimation(script.Anim)
                Anim:Play()
                local Sound = script.BarrageSound:Clone()
                Sound.Parent = Character.PrimaryPart
                Sound:Play()
                local Starttime = tick()
                repeat 
                    wait(.30)
                    CreateArm(Character)
                until tick() - Starttime >= 5 or not Character:FindFirstChild("Barraging")
                Character.PrimaryPart.StandPosition.Position = Vector3.new(-2,0.7,3)
                Anim:Stop()
                Sound:Destroy()
                Character.UsingMove.Value = false
            end


        else
            Character:FindFirstChild("Barraging"):Destroy()





        end

    end

end)

Answer this question