I Need A Little Help With Tables

--[[
-----what i want to know is will any args i put in the args table works depending on its function ? for string things it works but
  i want to know if something like this works too , if it does not work plesse tell me and correct this part because i usually want to work with args which are not
  strings
  and also , any better ways to use args without this args table ?-----
  _G.settings={
  Methods={
    functions={[1988]=function(...) game.Players.LocalPlayer.Character.PrimaryPart.Position=Vector3.new(...) end},
    args={[1988]={1,2,3}}
  }
}
local value='1988'
for i,v in next,settings.Methods.functions or {} do
  if value==tostring(i) then
    xpcall(function()
    v(table.unpack(settings.Methods.args[i]))
    end,
    function(err) warn('Error : '..err) end)
  end
end
  ]]
_G.settings={
  Methods={
    functions={[1988]=function(...)file=io.open('/storage/emulated/0/docs/lua.txt','w');file:write(...);file:close();file=io.open('/storage/emulated/0/docs/lua.txt','r');print(file.read(file,'*all'));file:close()end},
    args={[1988]={'this is an example arg'}}
  }
}
local value='1988'
for i,v in next,settings.Methods.functions or {} do
  if value==tostring(i) then
    xpcall(function()
    v(table.unpack(settings.Methods.args[i]))
    end,
    function(err) warn('Error : '..err) end)
  end
end

First of all, next time include your question out of the code, it becomes a little bit too complicated to read it if it’s within the code.

Second of all, pretty sure this code doesn’t work when trying to access the arguments as the first argument index is 1988, and your function goes from 1 to #functions. I say this because later on you try to index .args[i]. And v takes the value of a function, so… Kinda lost on the evaluation for value == tostring(i).

Now coming back to your question, there’s nothing wrong on passing a table as arguments to a function, even though a general structure for multiple arguments (less than 4) is to have them as tuples, example:

local function foo(first, second, third)
    print(first, second, third)
end

And for functions that require >= 4 arguments, it’s preferred to use tables as the only argument.

type TableArgument = { first: string, second: string, third: string, fourth: string }

local function foo(argument: TableArgument)
    local first = argument.first
    local second = argument.second
    ...

    print(first, second, third, fourth)
end

foo({ x = "Hello, world!" })

Now, I can see that your program is using io library, which is not permitted within the Roblox platform. Also, using _G for this type of scenarios just seems weird and unnecessary, try considering ModuleScripts.

I know io doesn’t exist in roblox , this is because currently i am on mobile and i am using io library as a test subject on an app called luaDroid, so this thing i asked is because i am trying to make an advanced keypad system to improve my lua knowledge. So i wanted to know if i can pass any kind of datas as the args in the args table depending on the its function, here is the code which i haven’t tested on pc since i dont have access to my pc now

_G.settings={
  TextWhenSuccess=nil,--string
  TextWhenIncorrect=nil,--string
  MaxDigits=nil,--number
  SecretEvents=nil--table -> {[1234]=function()print'1234'end} -> i=secretCode & v=function
}
local buttons=script.Parent
local textlabel=buttons.Parent.Part_That_The_SurfaceGui_Is_In.SurfaceGui.TextLabel
local value=''
local function RandomCode(MaxDigits)
  if MaxDigits and MaxDigits>18 then
    return error'Can Not Enter More Than 18 Digits'
  elseif MaxDigits and MaxDigits<=0 then
   return 0
 else
  local vary1,vary2=1,9;
  for i=1,(MaxDigits or 4)-1 do
    vary1=vary1..0;
    vary2=vary2..9;
  end
  return math.random(vary1,vary2)
 end
end
local code=RandomCode(settings.MaxDigits)
print(code)
for i,v in next,buttons:GetChildren() do
  if v:FindFirstChildOfClass'ClickDetector'==nil then
    Instance.new('ClickDetector',v)
  end
end
for i,v in next,buttons:GetChildren() do
  v:WaitForChild'ClickDetector'.MouseClick:Connect(function(plr)
    if (rawlen(value)==rawlen(tostring(code)) and v.Name:lower()~='enter' and v.Name:lower()~='backspace') then
      return
    end
    if v.Name:lower()=='enter' then
      if value==tostring(code) then
        value=''
        textlabel.Text=settings.TextWhenSuccess or 'Correct'
      elseif value~=tostring(code) then
        for i,v in next,settings.SecretEvents or {} do
          if value==tostring(i) then
            value=''
            textlabel.Text='SecretEvent Activated'
            coroutine.wrap(v)()
          elseif value~=tostring(i) then
            value=''
            textlabel.Text=settings.TextWhenIncorrect or 'Incorrect'
        end
        end
      end
    elseif v.Name:lower()=='backspace' then
      value=value:sub(1,rawlen(value)-1)
      textlabel.Text=value
    else
      value=value..tostring(v.Name)
      textlabel.Text=value
    end
  end)
end

As long as it’s not a function being passed over the network, or any kind of value that may be lost (like mixed tables) along the networking, functions can take whatever you want as arguments.

1 Like

Okay thanks for telling about that

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.