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

How can I modify the script so the ability of the sword doesn't create lag?

Asked by 2 years ago

I am remaking SFOTH swords and one of them has a ability where when you double click you do a lunge attack that Is followed by a poison bubble which grows bigger damaging enemies when touching It, It works but I was wondering how to prevent making lag, you see when I use the ability and Im Inside of the bubble (The ability won't harm you If your the owner) It will lag

Script (Server Script):

local Tool = script.Parent ----The tool was originally called Swuvle (And yes, Its a underground war thing) Who even names a sword "Swuvle"?
----It was also originally called "Poison Ive" cuz I used to want to rename the SFOTH swords (Ignore dis)
local SoundFolder = Tool.Sounds
local CanDoubleClickBoolValue = Tool:WaitForChild("CanDoubleClick")
local TimerBeforeYouCanNOTDoubleClick = 0.5
local SlashAttack
local Boolens = {
    Debounce = false,
    CanDamage = false
}
local Animations = { ---Oh boi there Is alot
    EquipAnimation = Tool.EquipAnimation,
    Idle = Tool.Idle,
    Slash = Tool.Slash,
    Lunge = Tool.Lunge
}

Tool.Equipped:Connect(function()
    SoundFolder.Unsheath:Play()
    local Humanoid = Tool.Parent.Humanoid

    local EquipAnimationTrack = Humanoid:FindFirstChild("Animator"):LoadAnimation(Animations.EquipAnimation)
    EquipAnimationTrack:Play()
    task.wait(0.30)
    local Idle = Humanoid:FindFirstChild("Animator"):LoadAnimation(Animations.Idle)
    Idle:Play()
    Tool.Unequipped:Connect(function()
        Idle:Stop()
    end)
end)

Tool.Activated:Connect(function()
    if not Boolens.Debounce then
        Boolens.Debounce = true
        Boolens.CanDamage = true
        SoundFolder.Slash:Play()
        print("The tool has been clicked")
        local Humanoid = Tool.Parent.Humanoid

        SlashAttack = Humanoid:FindFirstChild("Animator"):LoadAnimation(Animations.Slash)
        SlashAttack:Play()
        CanDoubleClickBoolValue.Value = true
        wait(TimerBeforeYouCanNOTDoubleClick)
        CanDoubleClickBoolValue.Value = false
        Boolens.CanDamage = false
    elseif Boolens.Debounce and CanDoubleClickBoolValue.Value then
        SlashAttack:Stop()
        print("The tool has been Double clicked")
        CanDoubleClickBoolValue.Value = false
        SoundFolder.Lunge:Play()
        SoundFolder.Slash:Stop()
        local Humanoid = Tool.Parent.Humanoid
        Humanoid.Jump = true
        Humanoid.WalkSpeed = 0

        local Lunge = Humanoid:FindFirstChild("Animator"):LoadAnimation(Animations.Lunge)
        Lunge:Play()
        wait(0.2)
        local VectorForce = Instance.new("VectorForce", Humanoid.Parent.Torso)
        local Attachment = Instance.new("Attachment", Humanoid.Parent.Torso)

        VectorForce.Force = Vector3.new(0,300,-9000)
        VectorForce.Attachment0 = Attachment
        VectorForce.ApplyAtCenterOfMass = true
        game:GetService("Debris"):AddItem(VectorForce,0.5)
        game:GetService("Debris"):AddItem(Attachment,0.5)
        wait(0.3)
        Humanoid.JumpPower = 0
        wait(0.20)
        SoundFolder.Explode:Play()
        local MassiveExplosion = Instance.new("Part") ---Fun fact: Originally a white explosion that knockbacks enemies
        MassiveExplosion.Parent = game.Workspace
        MassiveExplosion.Position = Tool.Handle.Position
        MassiveExplosion.Size = Vector3.new(1,1,1)
        MassiveExplosion.BrickColor = BrickColor.Green()
        MassiveExplosion.Shape = Enum.PartType.Ball
        MassiveExplosion.Anchored = true
        MassiveExplosion.CanCollide = false
        MassiveExplosion.CastShadow = false
        MassiveExplosion.Material = Enum.Material.ForceField
        wait(0.30)
        Humanoid.JumpPower = 50
        Humanoid.WalkSpeed = 16
        MassiveExplosion.Touched:Connect(function(hit)
            local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
            if Player then
                local player1 = game.Players:GetPlayerFromCharacter(Tool.Parent)
                local player2 = game.Players:GetPlayerFromCharacter(hit.Parent)

                if (player1 ~= nil) and (player2 ~= nil) then
                    local humanoid = (player2.Character or player2.CharacterAdded:Wait()):FindFirstChildWhichIsA("Humanoid")

                    if (player1.TeamColor ~= player2.TeamColor) then
                        if (humanoid ~= nil) then
                            humanoid.Health = humanoid.Health - 20
                            humanoid.WalkSpeed = 10
                            humanoid.JumpPower = 30
                            wait(5)
                            humanoid.WalkSpeed = 16
                            humanoid.JumpPower = 50
                        end
                    end
                end
            else
                local NPCHumanoid = hit.Parent:WaitForChild("Humanoid")
                if NPCHumanoid then
                    NPCHumanoid.Health = NPCHumanoid.Health - 20
                    NPCHumanoid.WalkSpeed = 10
                    NPCHumanoid.JumpPower = 30
                    wait(5)
                    NPCHumanoid.WalkSpeed = 16
                    NPCHumanoid.JumpPower = 50
                end
            end
        end)
        game:GetService("Debris"):AddItem(MassiveExplosion,3)
        while MassiveExplosion and wait(0) do
            MassiveExplosion.Size = MassiveExplosion.Size+Vector3.new(1,0,0)
            MassiveExplosion.Transparency = MassiveExplosion.Transparency + .01
        end
        wait(3)
        Boolens.Debounce = false
    end
    wait(1.5)
    Boolens.Debounce = false
end)

Tool.Handle.Touched:Connect(function(hit)
    local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if Player then
        local player1 = game.Players:GetPlayerFromCharacter(Tool.Parent)
        local player2 = game.Players:GetPlayerFromCharacter(hit.Parent)

        if (player1 ~= nil) and (player2 ~= nil) then
            local humanoid = (player2.Character or player2.CharacterAdded:Wait()):FindFirstChildWhichIsA("Humanoid")

            if (player1.TeamColor ~= player2.TeamColor) then
                if (humanoid ~= nil) and Boolens.CanDamage == true then
                    Boolens.CanDamage = false
                    humanoid:TakeDamage(10)
                    humanoid.WalkSpeed = 10
                    humanoid.JumpPower = 30
                    wait(5)
                    humanoid.WalkSpeed = 16
                    humanoid.JumpPower = 50
                end
            end
        end
    else
        local NPCHumanoid = hit.Parent:WaitForChild("Humanoid")
        if NPCHumanoid and Boolens.CanDamage == true then
            Boolens.CanDamage = false
            NPCHumanoid:TakeDamage(10)
            NPCHumanoid.WalkSpeed = 10
            NPCHumanoid.JumpPower = 30
            wait(5)
            NPCHumanoid.WalkSpeed = 16
            NPCHumanoid.JumpPower = 50
        end
    end
end)

Answer this question