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

Barrage Hitbox lags alot whats alternative?

Asked by 2 years ago

Hello, I have been working on a barrage system but I don't really know how to make the hitbox this is my current hitbox but its extremely lagy and destroys all fps

local function Detected(char)
    wait(.8)
    local hitbox = script.Meshes.HitBox:Clone()
    hitbox.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0,0,-2)
    hitbox.Parent = game.Workspace
    game.Debris:AddItem(hitbox,.08)


    local weld = Instance.new("ManualWeld")
    weld.Part0 = hitbox
    weld.Part1 = char.HumanoidRootPart
    weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
    weld.Parent = weld.Part0

    local connection 
    connection  = hitbox.Touched:Connect(function()end) 
    local results = hitbox:GetTouchingParts()

    coroutine.wrap(function()
        for i, obj in pairs(results) do
            if  not results[i]:IsDescendantOf(char)then
                local ehumanoid = results[i].Parent:FindFirstChild("Humanoid")
                local ehumanoidRP = results[i].Parent:FindFirstChild("HumanoidRootPart")
                if ehumanoid and ehumanoid.Health > 0 then
                    hitbox:Destroy()

                    if connection then
                        connection:Disconnect()
                    end

                    local vel = Instance.new("BodyVelocity",ehumanoidRP)
                    vel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
                    vel.Velocity = char.HumanoidRootPart.CFrame.lookVector * 4 + Vector3.new(0,.5,0)
                    game.Debris:AddItem(vel, .5)

                    ehumanoid:TakeDamage(2)
                    break
                end
            end
        end
    end)()

    task.delay(.08,function()
        if connection then
            connection:Disconnect()
        end

    end)

Answer this question