Hi. I am editing a able inside of a modulescript, but when I try to index anything it is nil when it clearly does exist.
local module = {}
local ServerScriptService = game:GetService("ServerScriptService")
local serverstats = ServerScriptService.ServerStats
local ev = serverstats.equippedvault
module.ownedvaults = {}
print(module.ownedvaults) -- this isnt nil
ev:GetPropertyChangedSignal("Value"):Connect(function()
if not module.ownedvaults[ev.Value] then -- but this is
table.insert(module.ownedvaults,ev.Value)
end
end)
what exactly is equippedvault? Like its class, and what does it hold?
If Its a ValueBase (like a StringValue, NumberValue, ObjectValue) , you should use .Changed instead of :GetPropertyChangedSignal(), its much more convenient
I understand why it gives you an error, it tells you that you are trying to index a null value, that’s why you should do
local module = {}
local ServerScriptService = game:GetService("ServerScriptService")
local serverstats = ServerScriptService.ServerStats
local ev = serverstats.equippedvault
module.ownedvaults = {}
print(module.ownedvaults) -- this isnt nil
ev:GetPropertyChangedSignal("Value"):Connect(function()
if module.ownedvaults ~= nil then
if not module.ownedvaults[ev.Value] then -- but this is
table.insert(module.ownedvaults,ev.Value)
end
else
table.insert(module.ownedvaults,ev.Value)
end
end)
I think that the issue is that module.ownedvaults is an array, not a dictionary. So it has numerical indexes instead of string keys that connect to values of any type(boolean, number, string, Instance, etc.). Perhaps you meant to use table.find(module.ownedvaults, ev.Value) that checks if the ev.Value exists in the array as a value, not as a key?(and it returns the first index it found it at)
You cannot search for an index that doesn’t exist.
You are using table.insert(module.ownedvaults,ev.Value) which is not indexing module.ownedvaults[ev.Value], you are rather inserting the ev.Value into the table with a numeric index rather then actually assigning it the ev.Value index
Assuming this is what you meant to do instead.
local module = {}
local ServerScriptService = game:GetService("ServerScriptService")
local serverstats = ServerScriptService.ServerStats
local ev = serverstats.equippedvault
module.ownedvaults = {}
print(module.ownedvaults) -- this isnt nil
ev:GetPropertyChangedSignal("Value"):Connect(function()
if not table.find(module.ownedvaults, ev.Value) then -- but this is
table.insert(module.ownedvaults,ev.Value)
end
end)
I will say with this method, once the table gets larger and larger and the table pools up, table.find will begin to cause some performance issues and may slow down. (Don’t worry about this, tables have to very big for this to become a concern)