Why is this skill so laggy when I use it

Damage:

local function Damage(Player, Skill, Part, Window)

task.spawn(function()

local Instances = {}

local Connection = Part.Touched:Connect(function(Hit)

if not table.find(Instances, Hit.Parent) then

table.insert(Instances, Hit.Parent)

local NewHit = nil

if Hit.Parent ~= nil then

local Human = Hit.Parent:FindFirstChild("Humanoid")

if Human then

NewHit = Human

end

local MobHuman = Hit.Parent:FindFirstChild("MobHumanoid")

if MobHuman then

NewHit = MobHuman

end

if NewHit then

if NewHit == Human then

local OpponentName = Hit.Parent.Name

local OpponentPlayer = game.Players:FindFirstChild(OpponentName)

if OpponentPlayer ~= nil then

local Data = OpponentPlayer:FindFirstChild("Data")

local Status = OpponentPlayer:FindFirstChild("Status")

if Data and Status then

local Blocking = Status:FindFirstChild("Blocking")

if Blocking then

if Blocking.Value == false then

if Status.Safe.Value == false and OpponentPlayer ~= Player then

if Human.Health > 0 then

if Part then

local HitSound = Part:FindFirstChild("Hit")

if HitSound then

if HitSound.IsPlaying == false then

HitSound:Play()

end

end

end

end

end

else

if Status.Safe.Value == false and OpponentPlayer ~= Player then

if Human.Health > 0 then

if Part then

local BlockedSound = Part:FindFirstChild("Blocked")

if BlockedSound then

if BlockedSound.IsPlaying == false then

BlockedSound:Play()

end

end

end

end

end

end

end

end

end

elseif NewHit == MobHuman then

local Blocking = Hit.Parent:FindFirstChild("Blocking")

if Blocking then

if Blocking.Value == false then

if Player.Status.Safe.Value == false then

if MobHuman.Health > 0 then

if Part then

local HitSound = Part:FindFirstChild("Hit")

if HitSound then

if HitSound.IsPlaying == false then

HitSound:Play()

end

end

end

end

end

else

if Player.Status.Safe.Value == false then

if MobHuman.Health > 0 then

if Part then

local BlockedSound = Part:FindFirstChild("Blocked")

if BlockedSound then

if BlockedSound.IsPlaying == false then

BlockedSound:Play()

end

end

end

end

end

end

end

end

if Player == LocalPlayer then

Remotes.Damage:FireServer(Skill, NewHit, Part)

end

end

end

end

end)

wait(Window)

Connection:Disconnect()

end)

end

Skill:

if Skill == "Kurohitsugi" then

local SkillOn = true

local Kurohitsugi = Resources["Kurohitsugi"]:Clone()

Kurohitsugi.Parent = Humanoid

Kurohitsugi.PrimaryPart = Kurohitsugi.Hitbox

Kurohitsugi:SetPrimaryPartCFrame(HumanoidRootPart.CFrame * CFrame.new(0,-Humanoid.HipHeight-Kurohitsugi.Hitbox.Size.Y/2,0))

local SavedCFrame = Player.Status["Camera CFrame"].Value

local OverlapFilter = {Character}

local QuickFilter = {"Map", "NPCS", "Assets", "Terrain", "Camera"}

local OverlapParameters = OverlapParams.new()

OverlapParameters.FilterType = Enum.RaycastFilterType.Exclude

for _,FilteredItemName in pairs(QuickFilter) do

local FilteredItemNameInstance = game.Workspace:FindFirstChild(FilteredItemName)

if FilteredItemNameInstance ~= nil then

table.insert(OverlapFilter, FilteredItemNameInstance)

end

end

OverlapParameters.FilterDescendantsInstances = OverlapFilter

for _, Part in pairs(game.Workspace:GetPartsInPart(Kurohitsugi.Main, OverlapParameters)) do

task.spawn(function()

local P

local Bind = false

if Part.Parent.Name ~= Player.Name and Part.Name == "HumanoidRootPart" then

local H = Part.Parent:FindFirstChild("Humanoid") or Part.Parent:FindFirstChild("MobHumanoid")

if H then

if H.Health > 0 then

if H.Name == "Humanoid" then

P = game.Players:GetPlayerFromCharacter(Part.Parent)

if P then

if P:FindFirstChild("Status") then

if P.Status:FindFirstChild("Blocking") then

if P.Status.Blocking.Value == false then

Bind = true

if P == LocalPlayer then

Remotes.Skill:FireServer(Skill, "Guard Broke", true)

end

end

end

end

end

elseif H.Name == "MobHumanoid" then

if Part.Parent:FindFirstChild("Blocking") then

if Part.Parent.Blocking.Value == false then

Bind = true

local GuardBroke = Part.Parent:FindFirstChild("Guard Broke")

if GuardBroke then

Remotes.StatChange:FireServer(Part.Parent, "Guard Broke", true)

end

end

end

end

end

if Bind == true then

local AnchoringPart = Instance.new("Part")

AnchoringPart.CanCollide = false

AnchoringPart.CanQuery = false

AnchoringPart.CanTouch = false

AnchoringPart.Transparency = 1

AnchoringPart.Name = "AnchoringPart"

AnchoringPart.Size = Vector3.new(1,1,1)

AnchoringPart.Parent = H

AnchoringPart.CFrame = Part.CFrame

Weld(Part, AnchoringPart, BlankCO)

AnchorPart(AnchoringPart)

AnchoringPart.Anchored = true

repeat wait() until SkillOn == false or H.Health <= 0

AnchoringPart:Destroy()

if H.Name == "Humanoid" then

if P then

if P == LocalPlayer and (not H:FindFirstChild("AnchoringPart")) then

Remotes.Skill:FireServer(Skill, "Guard Broke", false)

end

end

elseif H.Name == "MobHumanoid" then

if Part ~= nil and (not H:FindFirstChild("AnchoringPart")) then

local GuardBroke = Part.Parent:FindFirstChild("Guard Broke")

if GuardBroke then

Remotes.StatChange:FireServer(Part.Parent, "Guard Broke", false)

end

end

end

end

end

end

end)

end

--//Anchor Hit Box

task.spawn(function()

repeat

local HitsugiPart = Kurohitsugi.Hitbox:Clone()

HitsugiPart.Anchored = false

HitsugiPart.Name = Skill

HitsugiPart.Parent = Humanoid

HitsugiPart.CFrame = Kurohitsugi.Hitbox.CFrame

HitsugiPart.CanCollide = false

HitsugiPart.Size = Kurohitsugi.Hitbox.Size

HitsugiPart.Transparency = 1

Damage(Player, Skill, HitsugiPart, .35)

task.spawn(function()

repeat

HitsugiPart.AssemblyLinearVelocity = HitsugiPart.CFrame.UpVector * 150

RunService.Heartbeat:Wait()

until SkillOn == false

end)

task.spawn(function()

wait(1)

HitsugiPart:Destroy()

end)

wait(.5)

until SkillOn == false

end)

wait(.5)

if Player == LocalPlayer then

Remotes.Skill:FireServer(Skill, "Attacking", false)

end

wait(5)

SkillOn = false

Kurohitsugi:Destroy()

if Player == LocalPlayer then

Remotes.Skill:FireServer(Skill, "Stunning", false)

end

end

I have keybinds in playerscripts that fire the server in serverscriptsstorage which in turns fires the client in playerscripts finding the correct skill for the keybind.
the other skills arennt laggy on other attacks but this skill seems to affect my game as a whole.

I would suggest learning how to properly format your code, because this is purely unreadable.

1 Like

I honestly cant read it if it is formatted

I just used Lua Beautifier Online

First script
local function Damage(Player, Skill, Part, Window)

   task.spawn(function()

   local Instances = {}

   local Connection = Part.Touched:Connect(function(Hit)

   if not table.find(Instances, Hit.Parent) then

      table.insert(Instances, Hit.Parent)

      local NewHit = nil

      if Hit.Parent ~= nil then

         local Human = Hit.Parent:FindFirstChild("Humanoid")

         if Human then

            NewHit = Human

         end

         local MobHuman = Hit.Parent:FindFirstChild("MobHumanoid")

         if MobHuman then

            NewHit = MobHuman

         end

         if NewHit then

            if NewHit == Human then

               local OpponentName = Hit.Parent.Name

               local OpponentPlayer = game.Players:FindFirstChild(OpponentName)

               if OpponentPlayer ~= nil then

                  local Data = OpponentPlayer:FindFirstChild("Data")

                  local Status = OpponentPlayer:FindFirstChild("Status")

                  if Data and Status then

                     local Blocking = Status:FindFirstChild("Blocking")

                     if Blocking then

                        if Blocking.Value == false then

                           if Status.Safe.Value == false and OpponentPlayer ~= Player then

                              if Human.Health > 0 then

                                 if Part then

                                    local HitSound = Part:FindFirstChild("Hit")

                                    if HitSound then

                                       if HitSound.IsPlaying == false then

                                          HitSound:Play()

                                       end

                                    end

                                 end

                              end

                           end

                        else

                           if Status.Safe.Value == false and OpponentPlayer ~= Player then

                              if Human.Health > 0 then

                                 if Part then

                                    local BlockedSound = Part:FindFirstChild("Blocked")

                                    if BlockedSound then

                                       if BlockedSound.IsPlaying == false then

                                          BlockedSound:Play()

                                       end

                                    end

                                 end

                              end

                           end

                        end

                     end

                  end

               end

            elseif NewHit == MobHuman then

               local Blocking = Hit.Parent:FindFirstChild("Blocking")

               if Blocking then

                  if Blocking.Value == false then

                     if Player.Status.Safe.Value == false then

                        if MobHuman.Health > 0 then

                           if Part then

                              local HitSound = Part:FindFirstChild("Hit")

                              if HitSound then

                                 if HitSound.IsPlaying == false then

                                    HitSound:Play()

                                 end

                              end

                           end

                        end

                     end

                  else

                     if Player.Status.Safe.Value == false then

                        if MobHuman.Health > 0 then

                           if Part then

                              local BlockedSound = Part:FindFirstChild("Blocked")

                              if BlockedSound then

                                 if BlockedSound.IsPlaying == false then

                                    BlockedSound:Play()

                                 end

                              end

                           end

                        end

                     end

                  end

               end

            end

            if Player == LocalPlayer then

               Remotes.Damage:FireServer(Skill, NewHit, Part)

            end

         end

      end

   end

   end)

   wait(Window)

   Connection:Disconnect()

   end)

end
Second script
if Skill == "Kurohitsugi" then

   local SkillOn = true

   local Kurohitsugi = Resources["Kurohitsugi"]:Clone()

   Kurohitsugi.Parent = Humanoid

   Kurohitsugi.PrimaryPart = Kurohitsugi.Hitbox

   Kurohitsugi:SetPrimaryPartCFrame(HumanoidRootPart.CFrame * CFrame.new(0,-Humanoid.HipHeight-Kurohitsugi.Hitbox.Size.Y/2,0))

   local SavedCFrame = Player.Status["Camera CFrame"].Value

   local OverlapFilter = {Character}

   local QuickFilter = {"Map", "NPCS", "Assets", "Terrain", "Camera"}

   local OverlapParameters = OverlapParams.new()

   OverlapParameters.FilterType = Enum.RaycastFilterType.Exclude

   for _,FilteredItemName in pairs(QuickFilter) do

      local FilteredItemNameInstance = game.Workspace:FindFirstChild(FilteredItemName)

      if FilteredItemNameInstance ~= nil then

         table.insert(OverlapFilter, FilteredItemNameInstance)

      end

   end

   OverlapParameters.FilterDescendantsInstances = OverlapFilter

   for _, Part in pairs(game.Workspace:GetPartsInPart(Kurohitsugi.Main, OverlapParameters)) do

      task.spawn(function()

      local P

      local Bind = false

      if Part.Parent.Name ~= Player.Name and Part.Name == "HumanoidRootPart" then

         local H = Part.Parent:FindFirstChild("Humanoid") or Part.Parent:FindFirstChild("MobHumanoid")

         if H then

            if H.Health > 0 then

               if H.Name == "Humanoid" then

                  P = game.Players:GetPlayerFromCharacter(Part.Parent)

                  if P then

                     if P:FindFirstChild("Status") then

                        if P.Status:FindFirstChild("Blocking") then

                           if P.Status.Blocking.Value == false then

                              Bind = true

                              if P == LocalPlayer then

                                 Remotes.Skill:FireServer(Skill, "Guard Broke", true)

                              end

                           end

                        end

                     end

                  end

               elseif H.Name == "MobHumanoid" then

                  if Part.Parent:FindFirstChild("Blocking") then

                     if Part.Parent.Blocking.Value == false then

                        Bind = true

                        local GuardBroke = Part.Parent:FindFirstChild("Guard Broke")

                        if GuardBroke then

                           Remotes.StatChange:FireServer(Part.Parent, "Guard Broke", true)

                        end

                     end

                  end

               end

            end

            if Bind == true then

               local AnchoringPart = Instance.new("Part")

               AnchoringPart.CanCollide = false

               AnchoringPart.CanQuery = false

               AnchoringPart.CanTouch = false

               AnchoringPart.Transparency = 1

               AnchoringPart.Name = "AnchoringPart"

               AnchoringPart.Size = Vector3.new(1,1,1)

               AnchoringPart.Parent = H

               AnchoringPart.CFrame = Part.CFrame

               Weld(Part, AnchoringPart, BlankCO)

               AnchorPart(AnchoringPart)

               AnchoringPart.Anchored = true

               repeat wait() until SkillOn == false or H.Health <= 0

                  AnchoringPart:Destroy()

                  if H.Name == "Humanoid" then

                     if P then

                        if P == LocalPlayer and (not H:FindFirstChild("AnchoringPart")) then

                           Remotes.Skill:FireServer(Skill, "Guard Broke", false)

                        end

                     end

                  elseif H.Name == "MobHumanoid" then

                     if Part ~= nil and (not H:FindFirstChild("AnchoringPart")) then

                        local GuardBroke = Part.Parent:FindFirstChild("Guard Broke")

                        if GuardBroke then

                           Remotes.StatChange:FireServer(Part.Parent, "Guard Broke", false)

                        end

                     end

                  end

               end

            end

         end

         end)

      end

      --//Anchor Hit Box

      task.spawn(function()

      repeat

         local HitsugiPart = Kurohitsugi.Hitbox:Clone()

         HitsugiPart.Anchored = false

         HitsugiPart.Name = Skill

         HitsugiPart.Parent = Humanoid

         HitsugiPart.CFrame = Kurohitsugi.Hitbox.CFrame

         HitsugiPart.CanCollide = false

         HitsugiPart.Size = Kurohitsugi.Hitbox.Size

         HitsugiPart.Transparency = 1

         Damage(Player, Skill, HitsugiPart, .35)

         task.spawn(function()

         repeat

            HitsugiPart.AssemblyLinearVelocity = HitsugiPart.CFrame.UpVector * 150

            RunService.Heartbeat:Wait()

         until SkillOn == false

         end)

         task.spawn(function()

         wait(1)

         HitsugiPart:Destroy()

         end)

         wait(.5)

      until SkillOn == false

      end)

      wait(.5)

      if Player == LocalPlayer then

         Remotes.Skill:FireServer(Skill, "Attacking", false)

      end

      wait(5)

      SkillOn = false

      Kurohitsugi:Destroy()

      if Player == LocalPlayer then

         Remotes.Skill:FireServer(Skill, "Stunning", false)

      end

   end

What do you mean you “cant read it…” it is prolly the #1 cause of buggy, error-prone code. You need to be able to see what your working with for debugging, and you’re most likely going to look at this code again, and you know your future self won’t be able to understand this.

Welp I figured out it was the way I was handling the damage and I was using multiple large moving parts which caused a lot of lag, I redid some of the code and its a lot better now.

1 Like