improve this sword fight bot

-- Get the local player and their character
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

-- Wait for the necessary parts to load
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local sword = character:WaitForChild("Linked Sword")
local swordHandle = sword:WaitForChild("Handle") -- Get the sword's handle

local RunService = game:GetService("RunService")
local pointingConnection

-- Variables to hold reach calculations
local activationRange = 0
local armLength = 0
local swordLength = 0

-- Function to calculate the arm length
local function calculateArmLength()
    armLength = 0

    if character:FindFirstChild("RightUpperArm") then
        -- R15 Rig
        local upperArm = character:FindFirstChild("RightUpperArm")
        local lowerArm = character:FindFirstChild("RightLowerArm")
        local hand = character:FindFirstChild("RightHand")
        if upperArm and lowerArm and hand then
            armLength = upperArm.Size.Y + lowerArm.Size.Y + hand.Size.Y
        end
    elseif character:FindFirstChild("Right Arm") then
        -- R6 Rig
        local rightArm = character:FindFirstChild("Right Arm")
        if rightArm then
            armLength = rightArm.Size.Y
        end
    end
end

-- Function to calculate the sword length
local function calculateSwordLength()
    -- Get the largest dimension of the sword handle
    swordLength = math.max(swordHandle.Size.X, swordHandle.Size.Y, swordHandle.Size.Z)
end

-- Function to calculate the total activation range
local function calculateActivationRange()
    calculateArmLength()
    calculateSwordLength()
    local totalReach = armLength + swordLength
    activationRange = totalReach * 1.5 -- Adding a 10% buffer
end

-- Function to find the nearest player
local function findNearestPlayer()
    local closestDistance = math.huge
    local nearestPlayer = nil

    for _, otherPlayer in pairs(game.Players:GetPlayers()) do
        if otherPlayer ~= player then
            local otherCharacter = otherPlayer.Character
            if otherCharacter then
                local otherHumanoidRootPart = otherCharacter:FindFirstChild("HumanoidRootPart")
                if otherHumanoidRootPart then
                    local distance = (humanoidRootPart.Position - otherHumanoidRootPart.Position).Magnitude
                    if distance < closestDistance then
                        closestDistance = distance
                        nearestPlayer = otherPlayer
                    end
                end
            end
        end
    end

    return nearestPlayer
end

-- Rotate the character so the sword faces the nearest player and activate sword if in range
local function pointSwordAtNearestPlayer()
    local nearestPlayer = findNearestPlayer()
    if nearestPlayer then
        local targetCharacter = nearestPlayer.Character
        local targetHumanoidRootPart = targetCharacter:FindFirstChild("HumanoidRootPart")
        if targetHumanoidRootPart and targetHumanoidRootPart.Parent.Humanoid and targetHumanoidRootPart.Parent.Humanoid.Health > 0 then
            -- Get the sword's position
            local swordPosition = swordHandle.Position

            -- Get the direction from the sword to the target in the horizontal plane
            local direction = (targetHumanoidRootPart.Position - swordPosition)
            direction = Vector3.new(direction.X, 0, direction.Z).Unit

            -- Get the current forward direction of the character
            local currentLookVector = humanoidRootPart.CFrame.LookVector
            currentLookVector = Vector3.new(currentLookVector.X, 0, currentLookVector.Z).Unit

            -- Compute the angle between the current look vector and the desired direction
            local dotProduct = currentLookVector:Dot(direction)
            dotProduct = math.clamp(dotProduct, -1, 1) -- Clamp to avoid NaN errors
            local angle = math.acos(dotProduct)

            -- Determine the sign of the rotation
            local cross = currentLookVector:Cross(direction)
            local sign = (cross.Y >= 0) and 1 or -1

            -- Limit the rotation angle per frame for smoothness
            local maxRotationAngle = math.rad(20) -- Max 20 degrees per frame
            local rotationAngle = math.min(angle, maxRotationAngle)

            -- Apply the rotation
            humanoidRootPart.CFrame = humanoidRootPart.CFrame * CFrame.Angles(0, sign * rotationAngle, 0)

            -- Check if the target is within range
            local distance = (humanoidRootPart.Position - targetHumanoidRootPart.Position).Magnitude

            if distance <= activationRange then
                -- Activate the player's sword automatically
                sword:Activate()
            end
        end
    end
end

-- When the sword is equipped, start pointing at the nearest player
sword.Equipped:Connect(function()
    -- Calculate the activation range when the sword is equipped
    calculateActivationRange()

    pointingConnection = RunService.RenderStepped:Connect(function()
        pointSwordAtNearestPlayer()
    end)
end)

-- When the sword is unequipped, stop pointing
sword.Unequipped:Connect(function()
    if pointingConnection then
        pointingConnection:Disconnect()
        pointingConnection = nil
    end
end)