Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I got 2 different lines of code, Can someone help me understand their difference?

Okay so here it is..

game:GetService("TestService")

local game:GetService("TestService")

2 answers

At the first line you are accessing the TestService. At the 2nd line you are locating the TestService. Also you forgot to give the local variable a name.

local TestService = game:GetService("TestService")

(TestService doesn't exist so it should create a new one)

2 comments

0

Well...how do you know when to use local and when not to? Also would I only have to so ""local TestService =" if the service did not exist yet in the game. So if I used Work space instead of TestService, I would not have to use a variable local or not?

0

It is the same thing ;-;. Also you do not undererstand :GetService() at all.

game:GetService("TestService") is used when someone tries to use TestService once (in this this case it's TestService, but it could be Workspace, RunService or even MarketplaceService).

Example 1: let's use the Workspace service and change the baseplate's color:

game:GetService("Workspace").Baseplate.BrickColor = BrickColor.new("Black")

There I only change the color of the baseplate once, so it's fine to use game:GetService("Workspace") since I only need to access the Workspace service once to change the baseplate's color.

local VAR = game:GetService("TestService") is used when you need to access the service frequently (to save memory and CPU usage). This also creates TestService.

Example 2: Let's do the same as Example 1, but have it change color every second for a minute:

local workspace = game:GetService("Workspace")
local part = workspace.Baseplate

for i=0, 60 do
	wait(1)
	part.BrickColor = BrickColor.Random()
end

There the baseplate's color changes frequently, so I store both the Workspace and part as a variable, to access them quicker. This also gives better performance since I don't have to find the Workspace service and the baseplate every second.

This discussion is preserved as it was. New answers, comments, and votes are closed.