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

What is the best way to use local variables efficiently?

Asked by 6 years ago

I am scripting a tycoon and am wondering if I am using to many variables and want some tips on how to do it better. Or if it is the correct way to do it please tell me. Here is the variable section in a function:

local plr = hit.Parent.Parent
local plrName = plr.Name
local plrInfo = game.ServerStorage:FindFirstChild(plrName):WaitForChild("PlayerInfo")
local specialKey = "User_"..plr.UserId
local tycoon = script.Parent.Parent.Parent.Parent
local tycoonName = tycoon.Name
local savedPurchasedItemsTable  
local buildLevel = plrInfo.BuildLevel.Value 
local baseplate = findBaseplate(buildLevel)
local baseplateName = baseplate.Name
local tycoonBaseplate = game.Workspace.Tycoons:FindFirstChild(tycoonName):WaitForChild(baseplateName)
local serverBaseplate = game.ServerStorage.ModelTycoon:FindFirstChild(baseplateName)
0
I just don't know if it is good to use this many variables in one script. User#21908 42 — 6y
0
im not a scripter im a bad lua scripter but that dont look like alot of variables looks pretty good to me but also dont take what im saying im a horrible scripter Ecl0ox -7 — 6y
0
Do you really need all those variables? KingLoneCat 2642 — 6y
0
It would be helpful if I could keep them but I don't know if there is a better way. That is why I am asking the question. User#21908 42 — 6y
View all comments (2 more)
0
That's a fine amount of variables, except for something like `baseplateName = baseplate.Name` or plrName, those ones are just useless. tycoonName as well it seems theCJarmy7 1293 — 6y
0
Ok thank you User#21908 42 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

The maximum amount of variables you can have in a script is 200.

DEBUNK

Most people on SH think that local variables can be used anywhere in the script, they are WRONG. In Lua 5.1 (Roblox Lua's version of Lua), local variables MUST be used in their native block; otherwise they do not work.

0
What do you mean by native block? User#21908 42 — 6y
0
wat? TheeDeathCaster 2368 — 6y
0
By native block, I mean the indent the variables is assigned to, such as a while loop. DeceptiveCaster 3761 — 6y
Ad

Answer this question