Same, while doing “self” just looks cooler in my opinion, i would just do the top because i could change it at any time, and there wont be any weird context errors
Honestly, I would much rather just make variables.
In one of my viewmodel codes, literally everything is a variable beacuse its more neat.
--// services \\--
local service = game
local work = service:GetService("Workspace")
local rs = service:GetService("ReplicatedStorage")
local rn = service:GetService("RunService")
local plrs = service:GetService("Players")
--// declarables \\--
----------------------------------------------------------------------- replicated (declarables)
local events = rs.events
local remotes = events.remotes
local functions = events.functions
local gameplay = rs.gameplay
local inventory = gameplay.inventory
local player_f = gameplay.player
local tools = gameplay.tools
local weapons = gameplay.weapons
----------------------------------------------------------------------- player (declarables)
local plr = plrs.LocalPlayer
local char: Model = plr.Character or plr.CharacterAdded:Wait()
local hrp: Part = char:WaitForChild("HumanoidRootPart")
local human: Humanoid = char:WaitForChild("Humanoid")
local at: Animator = human:WaitForChild("Animator")
----------------------------------------------------------------------- functions (declarables)
local return_clone = functions:WaitForChild("return_clone")
by looking at my code, i make literally everything in a hierachy its own variable. This is so I dont always have to do stuff like:
function ya()
print(game:GetService("Players").LocalPlayer.Name)
end
function woo()
local a = 1
print(game:GetService("Players").LocalPlayer.Name, a)
end
and instead I could do:
local player = game:GetService("Players").LocalPlayer
function ya()
print(player.Name)
end
function woo()
local a = 1
print(player, a) -- i dont have to do all this "game.Players.LocalPlayer.Name" as its already assigned
end
Imports (GetService isn’t technically an import as that would be require, but it’s common for people on Roblox to treat it as such) shouldn’t be inside of the class itself since that’s very unconventional, even outside of Lua. So you should define services on top of the file.
only put necessary information into the object, if it is just a shortcut, put it at the top of the script. putting a shortcut into an object is just extra (doesn’t make sense either)
If this was outside of Lua, I would try to store all variables under the class. However, since Lua does not offer proper OOP tools such as access modifiers, you should only put in what is absolutely needed for your class, like PerceptualReality said. Having too many unnecessary variables in your class which can be unexpectedly modified by another file is dangerous.