When NPC does damage it lags the game

When my npc does damage it would lag the game.

DB = false

for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA(“BasePart”) then
v.Touched:Connect(function(hit)
if DB == true then return end
if hit == nil then return end
if hit.Parent == nil then return end
local vhum = hit.Parent:FindFirstChild(“Humanoid”)
if vhum ~= nil then
if hum.Health <= 0 then return end
if vhum ~= hum and not hit.Parent:FindFirstChild(“ZombieScript”) then – keeps it from hurting itself/ zombies
vhum:TakeDamage(math.random(1, 3))
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if vhum.Health <= 0 and plr then

				end
				DB = true
				task.wait(1)
				DB = false
			end
		end
	end)
end

end

2 Likes
DB = false

for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA(“BasePart”) then
v.Touched:Connect(function(hit)
if DB == true then return end
if hit == nil then return end
if hit.Parent == nil then return end
local vhum = hit.Parent:FindFirstChild(“Humanoid”)
if vhum ~= nil then
if hum.Health <= 0 then return end
if vhum ~= hum and not hit.Parent:FindFirstChild(“ZombieScript”) then – keeps it from hurting itself/ zombies
vhum:TakeDamage(math.random(1, 3))
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if vhum.Health <= 0 and plr then

				end
			end
                                DB = true
				task.wait(1)
				DB = false
		end
	end)
end
end
``
maybe moving the debounce can change something
1 Like

The debounce is put outside the loop meaning that it won’t actually function as intended, you may want to put it inside the loop, after if v:IsA(“BasePart”) then so it will work separately for different parts, reducing the lag.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.