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

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

Asked by 5 years ago
Edited 5 years ago

Okay so here it is..

game:GetService("TestService")

local game:GetService("TestService")

2 answers

Log in to vote
0
Answered by
valchip 789 Moderation Voter
5 years ago

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)

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? hawkeye1940 8 — 5y
0
It is the same thing ;-;. Also you do not undererstand `:GetService()` at all. valchip 789 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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.

Answer this question