
Key Required

local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local REFRESH_RATE = 0.5
local lastUpdate = 0
local TARGET_NAMES = {["civilian"] = true, ["hostile"] = true, ["pmc"] = true}
local COLORS = {
["civilian"] = Color3.fromRGB(0, 255, 0),
["hostile"] = Color3.fromRGB(255, 0, 0),
["pmc"] = Color3.fromRGB(255, 0, 0)
}
local function applyHighlight(model)
if model:FindFirstChild("ESPHighlight") then return end
local highlight = Instance.new("Highlight")
highlight.Name = "ESPHighlight"
highlight.Adornee = model
highlight.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
highlight.FillTransparency = 0.6
local color = COLORS[model.Name:lower()] or Color3.fromRGB(255, 255, 255)
highlight.FillColor = color
highlight.OutlineColor = color
highlight.Parent = model
end
local connection
connection = RunService.Heartbeat:Connect(function()
if os.clock() - lastUpdate >= REFRESH_RATE then
lastUpdate = os.clock()
for _, obj in ipairs(workspace:GetChildren()) do
if obj:IsA("Model") and TARGET_NAMES[obj.Name:lower()] then
applyHighlight(obj)
end
end
end
end)
script.Destroying:Connect(function()
connection:Disconnect()
for _, obj in ipairs(workspace:GetDescendants()) do
if obj.Name == "ESPHighlight" then
obj:Destroy()
end
end
end)



what is local CollectionService = game:GetService("CollectionService") for?
@BeboMods when I use workspace:GetDescendants() it basically searches the whole entire game for the models, when I used this it lagged the game for a quarter second whenever it refreshed to look for new or dead npcs. CollectionService is just a better and more optimized version to remove the small lag spikes that I had.
u just defined the local and never used it again tho?
@BeboMods The local variable is used to call GetTagged (to find existing NPCs) and GetInstanceAddedSignal (to find new NPCs the moment they spawn). It completely removes the need for a while wait() loop, which is why the lag disappears.
gpt'd