My damage script lags my game

debounce = false
script.Parent.Touched:Connect(function(part)
    if script.Parent.Parent.HpValue.Value == true and debounce == false then
        for k,v in pairs(script.Parent:GetTouchingParts()) do
            if v.Name == "HumanoidRootPart" then
    local ignore = script.Parent.Parent.Parent.HumanoidRootPart
    local ignore2 = script.Parent.Parent.HumanoidRootPart    
       if ignore ~= v then
    if ignore2 ~= v then
    local h = v.Parent:FindFirstChild("Humanoid")
    if h then
        end
    debounce = true
    
        h:TakeDamage(80)
        local move = Instance.new("BodyVelocity")
    move.MaxForce = Vector3.new(50000,50000,50000)
    move.Velocity = script.Parent.Parent.HumanoidRootPart.CFrame.lookVector * 60 + Vector3.new(0,5,0)
    move.Parent = h.Parent:FindFirstChild("HumanoidRootPart")
    h.Sit = true
    wait(0.2)
    move:Destroy()
    wait(1)
    end
    end
    end
        end
        debounce = false
       end
end)

my script lags my game alot everytime “HpValue” is set to true and it’s annoying

Using script.Parent:GetTouchingParts() is expensive are should not be called every time the part is touched. Instead just use the part that is passed as a parameter from the Touched event. So it should be like this:

debounce = false
script.Parent.Touched:Connect(function(part)
if script.Parent.Parent.HpValue.Value == true and debounce == false then
    if part.Name == "HumanoidRootPart" then
      local ignore = script.Parent.Parent.Parent.HumanoidRootPart
      local ignore2 = script.Parent.Parent.HumanoidRootPart
      if ignore ~= part and ignore2 ~= part then
          local h = part.Parent:FindFirstChild("Humanoid")
          if h then
          end
          debounce = true

          h:TakeDamage(80)
          local move = Instance.new("BodyVelocity")
          move.MaxForce = Vector3.new(50000,50000,50000)
          move.Velocity = script.Parent.Parent.HumanoidRootPart.CFrame.lookVector * 60 + Vector3.new(0,5,0)
          move.Parent = h.Parent:FindFirstChild("HumanoidRootPart")
          h.Sit = true
          wait(0.2)
          move:Destroy()
          wait(1)
        end
    end
  end
  debounce = false
end)

the problem is i have this ability in game where you could stop time and it anchors every player in the game except for the player who did it, and without :GetTouchingParts() the person who stopped time cannot deal any damage to other players who are frozen

So when time isn’t frozen, this lags the game? I’d suggest removing GetTouchingParts() and then when time is frozen, you can get distances to each of the players and then damage the players that are a certain distance away which would be much more efficient.

Wouldn’t be surprised. You’re using a relatively expensive method to try and find a Humanoid from a set of parts. You do not explicitly need to get the HumanoidRootPart, just check for a Humanoid in a model ancestor and then immediately flip on debounce if there is one.

local debounce = false

script.Parent.Touched:Connect(function (part)
    if script.Parent.Parent.HpValue.Value == true and not debounce then
        local humanoid = part.Parent:FindFirstChild("Humanoid")
        if humanoid and not humanoid:IsDescendantOf(script.Parent.Parent.Parent) then
            debounce = true
            -- The rest of your code here
        end
    end
end)

You have a bit of a strange setup in the sense that you need to do several parents. I don’t know where your script is in the grand scheme of things but it’d help to not bury it deep in the model hierarchy such that you have to write parent several times.

2 Likes