Anyway of doing this more efficiently?

I want to make this code more efficient but don’t know how:

  --Service's.
  
  local Workspace = game:GetService("Workspace")
  local RunService = game:GetService("RunService")
  local GuiService = game:GetService("GuiService")
  local Stats = game:GetService("Stats")
  local SoundService = game:GetService("SoundService")
  local LogService = game:GetService("LogService")
  local ContentProvider = game:GetService("ContentProvider")
  local KeyframeSequenceProvider = game:GetService("KeyframeSequenceProvider")
  local Chat = game:GetService("Chat")
  local Players = game:GetService("Players")
  local TimerService = game:GetService("TimerService")
  local MarketplaceService = game:GetService("MarketplaceService")
  local PointsService = game:GetService("PointsService")
  local AdService = game:GetService("AdService")
  local NotificationService = game:GetService("NotificationService")
  local ReplicatedFirst = game:GetService("ReplicatedFirst")
  local HttpRbxApiService = game:GetService("HttpRbxApiService")
  local TweenService = game:GetService("TweenService")
  local TextService = game:GetService("TextService")
  local PermissionsService = game:GetService("PermissionsService")
  local StarterPlayer = game:GetService("StarterPlayer")
  local StarterPack = game:GetService("StarterPack")
  local StarterGui = game:GetService("StarterGui")
  local LocalizationService = game:GetService("LocalizationService")
  local TeleportService = game:GetService("TeleportService")
  local JointsService = game:GetService("JointsService")
  local CollectionService = game:GetService("CollectionService")
  local PhysicsService = game:GetService("PhysicsService")
  local BadgeService = game:GetService("BadgeService")
  local Geometry = game:GetService("Geometry")
  local FriendService = game:GetService("FriendService")
  local InsertService = game:GetService("InsertService")
  local GamePassService = game:GetService("GamePassService")
  local Debris = game:GetService("Debris")
  local CookiesService = game:GetService("CookiesService")
  local UserInputService = game:GetService("UserInputService")
  local KeyboardService = game:GetService("KeyboardService")
  local MouseService = game:GetService("MouseService")
  local VRService = game:GetService("VRService")
  local ContextActionService = game:GetService("ContextActionService")
  local ScriptService = game:GetService("ScriptService")
  local AssetService = game:GetService("AssetService")
  local TouchInputService = game:GetService("TouchInputService")
  local BrowserService = game:GetService("BrowserService")
  local AnalyticsService = game:GetService("AnalyticsService")
  local  = game:GetService("")
  local Selection = game:GetService("Selection")
  local  = game:GetService("") (x4)
  local ServerScriptService = game:GetService("ServerScriptService")
  local ServerStorage = game:GetService("ServerStorage")
  local ReplicatedStorage = game:GetService("ReplicatedStorage")
  local LuaWebService = game:GetService("LuaWebService")
  local HapticService = game:GetService("HapticService")
  local MeshContentProvider = game:GetService("MeshContentProvider")
  local Lighting = game:GetService("Lighting")
  local SolidModelContentProvider = game:GetService("SolidModelContentProvider")
  local ChangeHistoryService = game:GetService("ChangeHistoryService")
  local  = game:GetService("")
  local NetworkServer = game:GetService("NetworkServer")
  local ClusterPacketCache = game:GetService("ClusterPacketCache")
  local ScriptContext = game:GetService("ScriptContext")
  local RuntimeScriptService = game:GetService("RuntimeScriptService")
  local HttpService = game:GetService("HttpService")
  local LanguageService = game:GetService("LanguageService")
  local TestService = game:GetService("TestService")
  local VirtualInputManager = game:GetService("VirtualInputManager")

Like I was thinking using metatables?

It’s already as efficient as it’s going to get, unless you decide to only require services that the script is going to use. You can’t even use AdService or BrowserService for example. If Roblox had any better of an IDE, it would tell you to get rid of most of those since they aren’t used later in the script.
And your solution to efficiency is pretty much never metatables. Metatables involve extra steps for both you to write and for the script to execute. Metatables are not extremely useful except for the odd OOP case or if you want to give your code away.

Just only declare the services that the script is actually going to use.

i saw someone use module script to make all their services appear automatically without declaring them Storing services in _G - #8 by Elttob

If you’re concerned about either cleanliness or efficiency, that’s not the way to go. It is of course your choice, but it’s messy and will be slower than just doing it per-script.

Removing this.

This is the most efficient your code can get, are you trying to make it more readable?

Remove this. Why are you defining nothing?

Also, unless you are writing a big script, you don’t need all those services. Use only the services the script needs.

1 Like