Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How come you have put 'game' before all services except workspace?

Asked by 10 years ago

You can access workspace with just the name Workspace.

print(Workspace)
--Output: Workspace

However, with other services, like Lighting or ServerStorage, you must put game before the word.

print(game.Lighting)
print(game.ServerStorage)
--Output: Lighting, ServerStorage

--

print(Lighting)
print(ServerStorage)
--Output: nil, nil

Why is this?

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

workspace is just a normal variable that ROBLOX has provided to us, like they have provided game.

For whatever reason, this is the only service that they give us a variable to, probably because it's the only one that almost any script requires.


If you want to define your own, you can, of course

local lighting = game.Lighting
local players = game.Players

etc.

ROBLOX suggests that you use the lowercase version of the workspace variable, since it's a Lua variable and Lua variables are canonically lowercase.


If you really want this without naming them yourself, you could use something like this:

local env = getfenv(0);
for _, service in pairs(game:GetChildren()) do
    local name = tostring(service);
    name = name:gsub("%W", "");
    env[name:sub(1,1):lower() .. name:sub(2)] = service
end

which will add all of the variables to be used, as in replicatedStorage or lighting.

Ideally you would wrap this into a module script / bindable function that would use a different number than 0 so that you just call that function in a single line and get all of the variables.

I would say this is a bad idea. ROBLOX's Script Analysis will warn that all of the service names were never defined.

Ad

Answer this question