Table not working properly?

Why does the table not function properly?
I call the index function to index it, and when i print self.Zones in the indexing function it prints properly, but printing self.Zones in the :checkTables() function it prints an empty table.

local zoneFolder = workspace.Zones

local zone = {}

zone.Zones = {}

function zone:indexZones()
	for _, zonePart in pairs(zoneFolder:GetChildren()) do
		if not zonePart:IsA("BasePart") then return warn("zonePart", tostring(zonePart), "Doesn't Exist.") end

		self.Zones[zonePart.Name] = {}

		zonePart.Touched:Connect(function(hit)
			local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

			if player and self.Zones[zonePart.Name][player.UserId] ~= true then
				self.Zones[zonePart.Name][player.UserId] = true
			end
		end)

		zonePart.TouchEnded:Connect(function(hit)
			local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			if player then
				self.Zones[zonePart.Name][player.UserId] = nil
			end
		end)
	end
end

function zone:checkTables(location, id)
        print(self.Zones) -- prints {}, Table doesn't include the zones indexed when :indexZone() is called.
	if zone.Zones[location][id] == true then
		return true
	else
		return false
	end
end

return zone

here’s how i call it:

if not zoneModule:checkTables("House", plr.UserId) then
4 Likes

Please provide the entire code. I do not see the function defined anywhere.

I made a syntax error. the function i call was the old one.

this is the entire function i think, was there anything else?

Do you ever call index zones? Try checking if it even runs.

It Runs. it’s called on a script in ServerSriptService.

Try printing self.Zones at the end of the indexzones thing. Also where do you call the check tables?

How do you require the zone module? Please share.

Also, please separately call

print(zoneModule.Zones)

Just one line above where you call your check function.

(Also I think you are trying to make a standard zone system. Why not consider ZonePlus?

It doesn’t look like you’re using metatables.

To use something like self, you would have to use metatables.

Do something like this

function zone.new()
   local self = setmetatable({}, {__index = zone})
   return self
end

Even when i don’t use self, the exact same things happen

I think you’re indexZone did not work. Did you test that it works?

you can see it correctly printing the tables in Handler:8

But then the error is not in line 9, which appears just after Handler:8. How come?

Please note, that is not true. For using self, you need to define your function with a : instead of . Metatables are completely unrelated.

handler:8 is the serverscript that runs :indexZones()