How to fix my anti-lag script?

Hi,
Im working on a script that should prevent lags. This script makes a sphere part welded to a player and if sphere touches a part with part that has a “Zone” tag its will check how much npc’s are in folder and if there less than 5 it will add more and if there more than 5 it will delete all.
But when i press Test its getting error:
Без названия
What Should i change in my script to make it work?

Script:

local CS = game:GetService("CollectionService")
local Zone = CS:GetTagged("Zone")
local sphere = game.ReplicatedStorage.Radius:Clone()
sphere.Parent = workspace
local weld = Instance.new("Weld")
weld.Parent = sphere
weld.Part1 = HumanoidRootPart
weld.Part0 = sphere
local folderchilds = workspace.NPCs:GetChildren()
local number = #folderchilds

while true do
    local PartsTouched = sphere:GetTouchingParts()
    for i, Part in pairs(PartsTouched) do
        if CS:HasTag(Part, "Zone") then
		    if number <= 5 then
			    workspace.NPCs:ClearAllChildren()
				wait()
				print("Added NPCs")
                game.ReplicatedStorage.Citizen:Clone().Parent = workspace.NPCs
			else
	            print("Removed NPCs")
	            workspace.NPCs:ClearAllChildren()
			end
        end
    end
end

You could start by adding a wait to your loop before the last end. You don’t have one, so you’re asking your script to run infinite iterations with no delay between each.

3 Likes