What is the best way to set up my localscript?

Hello. I was wondering what the best way to set up my local script would be regarding the “GAME_OBJS” variable. I want to be able to use the variable for any of my functions needed but is it necessary to share a variable with all of the functions or just make another variable inside of each function so that it has essentially the same variable but just used to that one function

Method 1:

--LocalScript
local contents = {}

["TEST"] = function(bool, information)
	local GAME_OBJS;

      if bool == true then
         if contents["GAME"] == nil then
            contents["GAME"] = {
				GAME_OBJS = {}; --ALL of the game objs. 
				game_Functions = {--ALL of the game functions.
                   ["FUNCTION1"] = function(bool)
                       GAME_OBJS["Counter1"] = 0;
                       while GAME_OBJS["Counter1"] == 0 do
                          wait(0.2)
                       end
                   end,
                 } 

               contents["GAME"].game_Functions["FUNCTION2"] = function()
                 GAME_OBJS["TopBar1"].Size = UDim2.new(0,0,0.05,0)
                 GAME_OBJS["TopBar2"].Size = UDim2.new(0,0,0.05,0)
               end 
            }
        end

      GAME_OBJS = contents["GAME"].GAME_OBJS
      GAME_OBJS.INFO = information
			
	  local info = GAME_OBJS.INFO 
	  if info["Command"] ~= nil then
	  	--Run a command then.
		  if info["Command"][1] ~= nil then
		  	  if contents["GAME"].game_Functions[info.Command[1]] ~= nil then
				  contents["GAME"].game_Functions[info.Command[1]](info["Command"][2])
			  end
		  else
			  if contents["GAME"].game_Functions[info.Command] ~= nil then
				  contents["GAME"].game_Functions[info.Command]()
			  end
		  end
	  end
    end	
end,


Method 2:

--LocalScript
local contents = {}

["TEST"] = function(bool, information)

     if bool == true then
         if contents["GAME"] == nil then
            contents["GAME"] = {
				GAME_OBJS = {}; --ALL of the game objs. 
				game_Functions = {--ALL of the game functions.
                   ["FUNCTION1"] = function(bool)
                      local GAME_OBJS = contents["GAME"].GAME_OBJS --**

                       GAME_OBJS["Counter1"] = 0;
                       while GAME_OBJS["Counter1"] == 0 do
                          wait(0.2)
                       end
                   end,
                 } 

               contents["GAME"].game_Functions["FUNCTION2"] = function()
                 local GAME_OBJS = contents["GAME"].GAME_OBJS  --**

                 GAME_OBJS["TopBar1"].Size = UDim2.new(0,0,0.05,0)
                 GAME_OBJS["TopBar2"].Size = UDim2.new(0,0,0.05,0)
               end 
            }
        end

      local GAME_OBJS = contents["GAME"].GAME_OBJS  --**
      GAME_OBJS.INFO = information
			
	  local info = GAME_OBJS.INFO 
	  if info["Command"] ~= nil then
	  	--Run a command then.
		  if info["Command"][1] ~= nil then
		  	  if contents["GAME"].game_Functions[info.Command[1]] ~= nil then
				  contents["GAME"].game_Functions[info.Command[1]](info["Command"][2])
			  end
		  else
			  if contents["GAME"].game_Functions[info.Command] ~= nil then
				  contents["GAME"].game_Functions[info.Command]()
			  end
		  end
	  end
   end	
end,


It’s kind of impossible to comment on which one is better structured because they’re both not very readable. What is this supposed to be, what are you trying to do and why?

What’s not readable about it? It’s a function that would be in my local script called from a remote event. Im trying to figure out the best to use my variable “GAME_OBJS”. The remote event will fire into the local script and then the command will go to whatever function is called

Would you post the rest of the code, specifically the parts that “call into” the localscript and the parts that use contents.GAME?

I think first one is your better bet its alot more formal to declare vars up top depending on scope, in your case since GAME_OBJS is scoped within only the loop it makes sense for it to be initialized before starting any of that other code yeah?

Sure.

--Server
script.RemoteEvent:FireAllClients("TEST", true, {Command = ("FUNCTION2");})
--Client:
local contents = {}
local funcs = { code here: https://devforum.roblox.com/t/what-is-the-best-way-to-set-up-my-localscript/1611375 }

game.Workspace.Script.RemoteEvent.OnClientEvent:Connect(function(command, val, val2, val3)
	if funcs[command] ~= nil then
		funcs[command](val, val2, val3)
	end
end)

And as another question. What would/could be bad about using the other method?

Sorry, it’s still really unclear to me :sweat_smile: The scripts from the OP seem to set up contents for something, but the latest code only works with funcs, so they’re pretty much unrelated?

To try and actually answer your question (as I understand it), variables should be generally be as “tightly” scoped as they can. The fewer parts of your code base that can read or especially write to a variable, the easier it will be to understand what does or doesn’t affect the various systems in the game.

Contents is used in the [“TEST”] function. ‘funcs’ holds all of the functions I need in my gui. I’m only concerned because I will be using some of this in a loop during a countdown so I am curious to know if my variable will be changed during the process. This would be an example of that:

Its more for formal and organization purposes. Everyone has their own style of formatting and its clear some prefer certain things over others. Its more like an etiquette sort of thing when you get to developing more and using different languages.

1 Like

i am going to be real with you I have absolutely no idea what you’re trying to accomplish here, but forward declaring a variable and using it in different functions works better in terms of consistency compared to declaring the object a different object every time you call a function