If you're doing OOP should you define services inside of the object or at the beginning of the script?

Like should you put

local Ts = game:GetService("TweenService")

in the beginning of your script. It seems more convenient but it seems like it’s not OOP, shouldn’t you put it inside of the object.

self.Ts = game:GetService("TweenService")

Then again youd need to do this for every object that uses tweenservice

2 Likes

I’d do it at the top personally.

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

But then when would you draw the line? Would you also put shortcuts on top? Like

local part = game.Workspace.Part

Its the same thing as a service basically

I feel like I would rather just put everything in the object to not worry about what to and what not to

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

Yeah but Im trying to do OOP to organize my code better, thats not doing OOP

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.

Ok I see, but shortcuts to things in the explorer like local part = game.Workspace.Part
would be defined in the object right?

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)

Alright got it

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.

1 Like