|
| 05 Feb 2017 02:55 PM |
*PLEASE READ BEFORE COMMENTING*
Hey, so I'm trying to add something to a table in a ModuleScript (which further gets added to a datastore), so I'm using table[key] = {value}, the problem is when I do that, it tells me the table index is nil.
I get that it probably wants me to use table.insert(), but the thing is, I need to be able to add keys other than just 1,2,3 etc., as other scripts use those keys to identify which value to change, and (to my knowledge) table.insert doesn't let you do that.
So what am I doing with by using table[key] = {value}, and how do I add a key AND a value to the table without erroring?
Thanks. |
|
|
| Report Abuse |
|
|
|
| 05 Feb 2017 02:57 PM |
post both the ModuleScript code and the script requiring it
|
|
|
| Report Abuse |
|
|
|
| 05 Feb 2017 03:04 PM |
--ModuleScript
indexes = {}
--Nonimportant Datastore stuff
function indexes.estabPoint(point,pointB, arg) indexes[point] = {pointB = arg} end
return indexes
--Script
function script.Parent.OnServerInvoke:connect(function(point,pointB,arg) --Yes, this is inside a RF, and yes I made sure it is being called, and none of the arguments are nil indexes.estabPoint(point,pointB,arg) --the arguments are provided from a TextBox end)
>ServerScriptService.IndexStatManager:44: table index is nil |
|
|
| Report Abuse |
|
|
|
| 05 Feb 2017 03:08 PM |
once a ModuleScript is required once per server/client, it'll always return the same thing it did when it was required that first time so even if you try to add another element to table indexes, it'll always return only indexes with function estabPoint
|
|
|
| Report Abuse |
|
|
|
| 05 Feb 2017 03:19 PM |
| Oh well that kinda sucks. Is there any way around that? |
|
|
| Report Abuse |
|
|
| |
|
|
| 08 Feb 2017 07:07 PM |
if you need the return value to be changed then just don't use modules
|
|
|
| Report Abuse |
|
|
| |
|
|
| 08 Feb 2017 07:11 PM |
"I need to be able to add keys"
once you require a module, it'll have the same return value per who required it (server/client)
don't use modules for this if you need whatever is returned to be changed
|
|
|
| Report Abuse |
|
|
|
| 08 Feb 2017 07:12 PM |
| Yes, but as I said, I'm using DataStores, which requires (unless I'm incorrect) use of Modules. |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2017 07:12 PM |
what gave you that idea? I'd recommend reading up on data stores
|
|
|
| Report Abuse |
|
|
|
| 08 Feb 2017 07:14 PM |
| There's a table and I need scripts to access it. And it needs to save it's data across sessions, ergo the ModuleScript is needed. |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2017 07:15 PM |
(Sorry for double post)
It should be the other way around, it needs a table with scripts accessing it, so a Module is needed. |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2017 07:15 PM |
TL;DR on replies
If you're trying to access a table that's in 1 script, from another script, that won't work unless you use global variables or some other form of transferring variables.
Q: how do I add a key AND a value to the table without erroring? A: First, make sure the table exists.
local t = {}
Second, choose your key name.
local key = "chedder"
Thirdly, choose your value's, er.. Value.
local value = 100
Finally, set your key's value within your table.
t[key] = value
If you're using a dictionary then this is one of the only ways to my knowledge. If this is wrong then someone please correct me. |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2017 07:15 PM |
I don't think you understand the functionality of ModuleScripts at all what makes you think they're required for data stores?
|
|
|
| Report Abuse |
|
|
|
| 08 Feb 2017 07:17 PM |
| Apparently you are wrong because that (sometimes) results in a table index is nil error. It isn't now for whatever reason, but I still need some way of transferring variables. Can the require function not be "refreshed" or whatever? |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2017 07:22 PM |
| You know what I'm going to try using _G and see how it works. |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2017 07:23 PM |
I still don't know why you're hesitant to just handle it from a server script
|
|
|
| Report Abuse |
|
|