【问题标题】:How to optimize cloning in new roblox update如何在新的 roblox 更新中优化克隆
【发布时间】:2021-09-18 13:10:11
【问题描述】:

在新的 roblox 更新之前一切都很顺利,现在我的弹幕效果克隆让游戏变得疯狂地滞后。

任何关于优化它的帮助将不胜感激。

我知道这听起来可能很懒,但如果你能为我优化它会很棒。我对如何优化一无所知,这是我最后的希望。

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)

【问题讨论】:

  • 这不是编码服务。请介绍你所做的和你发现的。这些滞后不太可能是由您的所有代码引起的。缩小范围!
  • 我对 roblox 一无所知,但如果它没有性能基准测试工具,我会感到惊讶。也许在平台上更有知识的人可以指出这些?

标签: optimization lua roblox cloning


【解决方案1】:

与其从存储中克隆它,不如从一开始就克隆一堆并将它们保持在很高的天空中,这样它就不会被渲染,并且当你需要它们时,在切换它们时相应地定位它们。

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 2017-04-25
    • 2016-06-09
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 2010-11-28
    • 2019-07-04
    • 2011-04-12
    相关资源
    最近更新 更多