Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Insert a table from different script?

Asked by 3 years ago
Edited 3 years ago

can you insert a table from different script? for example = script 1 : list = {} script 2 : insert integer data inside "list" from script 1

0
You might be able to do that with a module script. Cynical_Innovation 595 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

You can use a ModuleScript that can be used to hold data.

ModuleScript is basically a storage script, it can be used as a ServerScript, if you use it correctly, but the main use is to holster functions that are used multiple times in multiple scripts, including tables, variables, and the functions.

I also recommend storing ModuleScripts in ReplicatedStorage so LocalScripts and ServerScripts can access the module, as ServerScriptService cannot be accessed from LocalScripts.

ModuleScript

local tableModule = {"A","B","C"}

return tableModule

Now, if you use a LocalScript or ServerScript and call that, like this.

local mod = require(game:GetService("ReplicatedStorage").ModuleScript)

print(mod[2])

This will return B since we are indexing the table by 2

You can also do the same thing with functions.

local tableModule = {"A","B","C"}

tableModule:GetLetter(num)
    return tableModule[num]
end

return tableModule

In a LocalScript or ServerScript

local mod = require(game:GetService("ReplicatedStorage").ModuleScript)

print(mod:GetLetter(1))

This will print A, since we are indexing the table with 1


If you want to go somewhat in-depth about ModuleScripts, look at my post below. It also explains LocalScripts, ServerScripts, ModuleScripts, and there's a post on there about RemoteEvents.

Script Post w/ RemoteEvents

Ad

Answer this question