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

What is the best way to use local variables efficiently?

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)

6 comments

0

I just don't know if it is good to use this many variables in one script.

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

0

Do you really need all those variables?

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.

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

0

Ok thank you

1 answer

✓ Accepted answer

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.

3 comments

0

What do you mean by native block?

0

wat?

0

By native block, I mean the indent the variables is assigned to, such as a while loop.

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