I’m not sure how to explain this exactly so I will show an example
Is it possible to do this in roblox studio? If so, how would I do so?
I already know I can do it on one line but I wanted to know if I could do multiple lines
You can also make a function that returns a tuple and assign that to the variable which (I think) would be closer to the require approach:
local function getServices(): (Players, ReplicatedStorage, ServerStorage)
return game:GetService('Players'), game:GetService('ReplicatedStorage'), game:GetService('ServerStorage')
end
local players, replicatedStorage, serverStorage = getServices()
Thanks, quick question though.
What is the : (Players, ReplicatedStorage, ServerStorage) part?
Would it allow me to customize whats returned?
Like
--if I want all of them
local players, replicatedstorage, serverstorage = getServices()
--if I just want/need players and serverstorage
local players, serverstorage = getServices()
That’s just type checking, it doesn’t exist in standard Lua and it’s completely optional but it might make autocomplete work better if you’re doing complex things with the object, or the type somehow gets lost somewhere and it’s redefined as a table instead of the service. It doesn’t change any functionality.
If you wanna get into type checking, here’s a good topic:
If you’re looking for a function that changes what services are returned, you could probably look into a variadic function but you still need to pass the arguments of what is to be returned if that makes sense.
local function getServices(...) -- if we didn't pass a string here, we wouldn't get any return because the length of the packedServices table would be 0
local packedServices = {...} -- wrap the to-be-returned services in a table
local returnedServices = {}
for i, serviceName in ipairs(packedServices) do
table.insert(returnedServices, game:GetService(serviceName)
end
return unpack(returnedServices) -- "unwraps" the services from their container table
end
local replicatedStorage, serverStorage = getServices('ReplicatedStorage', 'ServerStorage')