Table being nil

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

1 Like

It is a stringvalue.

gfhkjhgghhjkl

How about putting a print to see how far it goes:

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)
2 Likes

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)

this is the solution i apologize i thought you were indexing a dictionary

Make sure to mark the solution if that is what was needed!

oh no nvm thanks this worked i just had to use table.find again

Ah kk. I was gonna explain it a bit more! If you need me to explain how tables work please let me know.

Hi. I recently came across this problem again. Even with this its saying that the table doesn’t exist.

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