Assigning variables, tables and different values from a function

Hello everyone watching this topic, I came across a problem when I was trying to assign a value in a function, in example I call a variable from a script, and run a function in another scripts that changes the variable that I called.

The issue here is, when I run the code, trying to print the variable value given, and it returns the same value I put to call the variable.

I tried multiple solutions such as using return (GivenValue) but still not working, I didn’t look at the devforum yet, but I will look at it after this post and warn it here if I found a solution for it.

Anyways, here’s some examples about the code I made: (This is an example btw)

Module script that has the function:

local module = {
        ["AssignTable"]  = function(Val1, Val2, Val3, tablegiven)
        tablegiven = {"First Value is: " .. Val1, "Second Value is: " .. Val2, "Third Value is: " .. Val3,}
        return tablegiven
        end)

}

return module

Whatever script:

local tableVal = {}
local modulescript = require(ModulePath) -- Where the module is located

modulescript.AssignTable(1, 2, 3, tableVal)
print(tableVal) -- Should print the given table in the function in my point of view, but only prints "{}", the initial value of the variable

If someone here knows the issue, you can reply to this post, if I did something wrong or if functions shouldn’t be used to make what I did. Thanks!

You shouldn’t be making functions inside the module. To access the functions outside the module, do this:

local module = {
  ["Any data here"] = "aaa"
}
--Add this
function module.AssignTable(Val1, Val2, Val3, tablegiven)
  --Functionallity
end

return module

Keep in mind, this will run on the server or client depending on which script you require it from.

Oh! Just noticed what your issue is. Sorry! :sweat_smile:
I think the reason this isn’t working is because you cant assign variables to function inputs. I could be wrong, but for module scripts I dont think so. Try this:

local module = {}

function module.AssignTable(Val1, Val2, Val3, tablegiven)
  --Then you could use tablegiven with this to do cool stuff!
  local table_ = {"First Value is: " .. Val1, "Second Value is: " .. Val2, "Third Value is: " .. Val3}
  return table_
end

return module

Hi! Just used your script format with mine and it did actually assign the value, just by modifying a bit the script to this:

tableVal = modulescript.AssignTable(1, 2, 3) -- And removed the fourth value bacuse it seemed to be useless

Anyways, thanks for the solution! I will mark this as a solution.

1 Like

Oops, didn’t see that! Good call! Good luck on your development journey!:grinning:

1 Like

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