I understand what I'm doing wrong and why it's high in Recv but I don't know how to get around this issue or how to fix it. There is around 20 NPC's on the map with this Script.
--EDIT-- The Script Checks if the player is within Range and Attacks the player and put's a Sound of the NPC hitting them but this Checks all the time and it's making the game lag when there's a lot of NPC's. The Line of code that I figure out that was making this issue is this..
local Players = game.Players:GetPlayers() for i,Player in ipairs (Players) do local char = Player.Character local Plr = Player if char ~= nil then local hum = char:FindFirstChild("Humanoid") if char.UpperTorso ~= nil then local Plu = char.UpperTorso
With this it's checking all Players ingame all the time and Rising the Recv end to 100-1000ms, This is what I'm trying to get help with, I like to know if there's a diff way to go around using that so I can check if the player is within Range or not but not have it run a 100 times within a second. There's no errors with the script.
`local NPC = script.Parent.Parent local Humanoid = NPC:WaitForChild('Zombie') repeat wait() until script.Parent.Parent.Torso local Damage = game.ReplicatedStorage.BoarConfigs.BoarDamage.Value
local COOLDOWN = 3 local h = false
while wait() do
local Players = game.Players:GetPlayers()
for i,Player in ipairs (Players) do
local char = Player.Character
local Plr = Player
if char ~= nil then
local hum = char:FindFirstChild("Humanoid")
if char.UpperTorso ~= nil then
local Plu = char.UpperTorso
local m = (script.Parent.Parent.HumanoidRootPart.Position - Plu.Position).magnitude
if m <=6 then
if h == false and hum.Health >0 then
if Plu ~= nil then
local Def = char.Def
local SDef = char.SDef
local MainDef = Def.Value + SDef.Value
local NewDamage = Damage - MainDef
local asound = Instance.new("Sound", char.Head)
asound.SoundId = "http://www.roblox.com/asset/?id=367499850"
asound.Name = "MobAttack"
asound.Volume = 0.3
asound.PlaybackSpeed = 0.3
asound:Play()
hum.Health = hum.Health - NewDamage
Humanoid.WalkSpeed = 13
h = true
wait(COOLDOWN)
char.Head.MobAttack:Destroy()
Humanoid.WalkSpeed = 16
h = false
else
print("Player has left the game or isn't in Range")
end
end
end
end
end
end
end`
If anyone can give me a hint on a way around this or let me know how too would be great.

3 comments
What's the error you're getting? What does this script do to the NPCs?
Need more context
I updated the post to give more info on what the script does and what I mean.