Are you able to use `table.insert` on a module script?

Hello, I was wondering is you can use table.insert On another script.
For an Example:
Let’s say I am using a Module script, and a Script Requires the Module.
And I use that Script to do table.insert on that module script.

Like this example:

local ExampleModule = require(workspace.ModuleScript) --Requires the Module

function InsertTable(Data) --Loads the function
    table.insert(ExampleModule.UserData,1,Data)
end

InsertTable("Text") --Does the function what it was told to do.

I was unsure if you can use table.insert on a module script. so that’s why I did this post.

2 Likes

Sure you can. But it won’t actually insert it to the table within the module, just in the version of that table which exists in the script you required the module from— so if yo I require the module again, the table you receive won’t have the inserted value.

No all serverscripts share one version of the module, and same for the client.

2 Likes

@bacionhairmanfur2

It will, and when the module is required again it’ll contain the value inserted as well.

# module
# assume # to be -- (for comments)

local f = {}

return f

# server-script

local module = require(path)
table.insert(module, "string")

# yield in another server-script, then require the module (yield because the order in which scripts will exist is not known)
wait(5)
print(table.find(require(module), "string") ~= nil) 
# prints true

print(unpack(require(script.Parent.Parent)))
# prints "string"
2 Likes

oh damn ok that’s cool, didn’t know that