generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Workspace or game:GetService("Workspace")?

Previous Thread :: Next Thread 
dennis96411 is not online. dennis96411
Joined: 06 Mar 2009
Total Posts: 1079
15 Aug 2013 06:23 PM
I know that using game:GetService("Players") is more reliable than game.Players since there's no direct variable set as the Players DataModel, but what about Workspace?
I did some testing in the studio and it seems that "Workspace" always points to the Workspace DataModel, regardless of its name. To me, using GetService to find any DataModel is safer, but I'm just being paranoid.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
15 Aug 2013 06:26 PM
I wouldn't say "more reliable."
Report Abuse
dennis96411 is not online. dennis96411
Joined: 06 Mar 2009
Total Posts: 1079
15 Aug 2013 06:28 PM
I would, when using game:GetService("Players") compared to using game.Players. I'm writing a script to use in script builder games, and anyone can rename the DataModels, making them inaccessible without using GetService. What I need to know is whether "Workspace" will always point to the Workspace DataModel regardless of its name.
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
15 Aug 2013 06:29 PM
Code in the player and code in the workspace don't necessarily run without the services they are parented to
Report Abuse
ZachBloxx is not online. ZachBloxx
Joined: 26 Jun 2013
Total Posts: 2833
15 Aug 2013 06:31 PM
Yes, it will always point to workspace no matter it's name. At least from my experience that's what it has been.
Report Abuse
blocco is not online. blocco
Joined: 14 Aug 2008
Total Posts: 29474
15 Aug 2013 06:32 PM
Use `Workspace`, `workspace`, `Game.Workspace`, `Game.workspace`, `game.Workspace` or `game.workspace`. `game:GetService("Workspace")` is unnecessary.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
15 Aug 2013 06:32 PM
It makes it safer in weird environments, not reliable.
Report Abuse
ColorfulBody is not online. ColorfulBody
Joined: 17 Jun 2012
Total Posts: 2353
15 Aug 2013 06:32 PM
Use the Workspace variable. Example:

local Lighting = Game:GetService('Lighting')

while true do
  if Workspace.Wall.Enabled.Value then
    Lighting:SetMinutesAfterMidnight(Lighting:GetMinutesAfterMidnight() + wait())
  else
    wait()
  end
end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
15 Aug 2013 06:33 PM
y u no just:

x = wait()
+ x
remove the else

jus an xmple
Report Abuse
blocco is not online. blocco
Joined: 14 Aug 2008
Total Posts: 29474
15 Aug 2013 06:34 PM
`game.lighting` == `game.Lighting` == `Game.lighting` == `Game.Lighting`

no need for `Game:GetService("Lighting")`
Report Abuse
woot3 is not online. woot3
Joined: 10 Nov 2008
Total Posts: 3599
15 Aug 2013 06:35 PM
I normally use game.Workspace, it's an old habit I picked up.
Report Abuse
ColorfulBody is not online. ColorfulBody
Joined: 17 Jun 2012
Total Posts: 2353
15 Aug 2013 06:35 PM
You should always use "Workspace" because it is the most efficient (it doesn't require a C call, unlike DataModel::GetService and it doesn't require to index a userdata, like Game.Workspace). It isn't more reliable, though.

Game.Workspace, game.Workspace, game.workspace, Game.workspace, game:GetService('Workspace'), Game:GetService('Workspace'), workspace and Workspace are all just as reliable. GetService will always return the workspace, Workspace will always point to the workspace and Game.Workspace will always be the workspace even if it is renamed because 'Workspace' is a property of the game object.
Report Abuse
blocco is not online. blocco
Joined: 14 Aug 2008
Total Posts: 29474
15 Aug 2013 06:35 PM
`game.Lighting` actually doesn't always work. But `game.lighting` is deprecated and will work for now.
Report Abuse
blocco is not online. blocco
Joined: 14 Aug 2008
Total Posts: 29474
15 Aug 2013 06:36 PM
The API is so safe here that even noobs can use workspace
Report Abuse
dennis96411 is not online. dennis96411
Joined: 06 Mar 2009
Total Posts: 1079
15 Aug 2013 06:40 PM
blocco, I need to use the script in script builder games which means the environment is "unsafe" like someone else said. Anyone can rename the DataModels, making them inaccessible without the use of GetService.

print(game.Lighting) --> "Lighting"

game.Lighting.Name = "LOL"

print(game.Lighting) --> "Lighting is not a valid member of DataModel"
Report Abuse
blocco is not online. blocco
Joined: 14 Aug 2008
Total Posts: 29474
15 Aug 2013 06:42 PM
I mentioned that, dennis
Report Abuse
ColorfulBody is not online. ColorfulBody
Joined: 17 Jun 2012
Total Posts: 2353
15 Aug 2013 06:43 PM
> `game.Lighting` actually doesn't always work. But `game.lighting` is deprecated and will work for now.

You shouldn't use it, though, precisely because it is deprecated.
Report Abuse
ColorfulBody is not online. ColorfulBody
Joined: 17 Jun 2012
Total Posts: 2353
15 Aug 2013 06:46 PM
> blocco, I need to use the script in script builder games which means the environment is "unsafe" like someone else said. Anyone can rename the DataModels, making them inaccessible without the use of GetService.

Services are not "DataModels". The object referenced by the `Game` and `game` global variables is the DataModel.
Report Abuse
Quenty is not online. Quenty
Joined: 03 Sep 2009
Total Posts: 9316
15 Aug 2013 06:49 PM
ColorfulBody is right.

Always use Workspace (or workspace) depending on your coding standards.

Of course, GetService and game.Workpace work, but aren't as efficient, and are harder to read. I've got something like this at the top of most of my scripts:

local Players = Game:GetService('Players')
local StarterPack = Game:GetService('StarterPack')
local StarterGui = Game:GetService('StarterGui')
local Lighting = Game:GetService('Lighting')
local Debris = Game:GetService('Debris')
local Teams = Game:GetService('Teams')
local BadgeService = Game:GetService('BadgeService')
local InsertService = Game:GetService('InsertService')
local Terrain = Workspace.Terrain
Report Abuse
dennis96411 is not online. dennis96411
Joined: 06 Mar 2009
Total Posts: 1079
15 Aug 2013 07:10 PM
This is odd. I renamed Workspace to something else and both "Workspace" and "game.Workspace" were still able to locate it. If I renamed any other services then they can't be located using their original names.
Report Abuse
blocco is not online. blocco
Joined: 14 Aug 2008
Total Posts: 29474
15 Aug 2013 07:16 PM
@dennis: the DataModel singleton has Workspace and workspace as members
Report Abuse
ZachBloxx is not online. ZachBloxx
Joined: 26 Jun 2013
Total Posts: 2833
15 Aug 2013 07:16 PM
It's not surprising. Considering Workspace is the only service under the Data Model that can be accessed without writing out the Data Model parent.

Also, I think Quenty's Gui Admin Commands change the name of Workspace.
Report Abuse
dennis96411 is not online. dennis96411
Joined: 06 Mar 2009
Total Posts: 1079
15 Aug 2013 07:19 PM
But the others aren't members? Kind of odd how they included only Workspace.
Report Abuse
ZachBloxx is not online. ZachBloxx
Joined: 26 Jun 2013
Total Posts: 2833
15 Aug 2013 07:23 PM
Well Workspace is used a lot. It would get annoying if you would have to type game.Workspace each time you wanted to access it.
Report Abuse
xSIXx is not online. xSIXx
Joined: 06 Aug 2010
Total Posts: 9202
15 Aug 2013 08:00 PM
"Well Workspace is used a lot. It would get annoying if you would have to type game.Workspace each time you wanted to access it."

i assume you would put it in a variable...
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 2Go to page: [1], 2 Next
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image