local RunService = game:GetService("RunService") local LogService = game:GetService("LogService") local ScriptContext = game:GetService("ScriptContext") local Debris = game:GetService("Debris") local Workspace = game:GetService("Workspace") -- Configuration local DEBUG_MODE = false local CHECK_INTERVAL = 10 local MAX_CONNECTIONS = 100 -- Max connections per service before pruning local EFFECT_CLEANUP_TIME = 5 local function disconnectNonForeign(service, eventName) local connections = getconnections(service[eventName]) local count = 0 for _, c in ipairs(connections) do if not c.ForeignState then c:Disconnect() count = count + 1 end end if DEBUG_MODE and count > 0 then print(string.format("Disconnected %d non-foreign %s connections", count, eventName)) end end local function pruneConnections(service, eventName) local connections = getconnections(service[eventName]) if #connections > MAX_CONNECTIONS then for i = #connections, MAX_CONNECTIONS + 1, -1 do local c = connections[i] if not c.ForeignState then c:Disconnect() end end if DEBUG_MODE then print(string.format("Pruned %s connections to %d", eventName, MAX_CONNECTIONS)) end end end local function cleanupEffects() for _, obj in ipairs(Workspace:GetDescendants()) do if obj:IsA("ParticleEmitter") or obj:IsA("Trail") or obj:IsA("Beam") then if not obj.Parent:FindFirstChildOfClass("Humanoid") then Debris:AddItem(obj, EFFECT_CLEANUP_TIME) end end end end disconnectNonForeign(LogService, "MessageOut") disconnectNonForeign(ScriptContext, "Error") local lastCheck = tick() RunService.Heartbeat:Connect(function() if tick() - lastCheck >= CHECK_INTERVAL then -- Re-check connections to catch new ones disconnectNonForeign(LogService, "MessageOut") disconnectNonForeign(ScriptContext, "Error") pruneConnections(RunService, "Heartbeat") pruneConnections(RunService, "Stepped") cleanupEffects() lastCheck = tick() end end) cleanupEffects() if DEBUG_MODE then print("DEBUG MODE ENABLED)") else print("LAG FIX IMPLEMENTED") end