Tables with the same variables problem

my table is like this:

[UserId] = {houseZone,houseNumber}

so if player have two houses it will look like this:

[UserId] = {houseZone,1}
[UserId] = {houseZone,2}

if i want make loop to remove house and this variable from table it will do it only for one house cause loop finding variable by user id

How can i fix it to do loop for all variables? this is my loop:

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(houseZones[2]) do
		if houseZones[player.UserId] then
			houseZones[player.UserId][1].Parent:Destroy()
		end
	end
end)

It looks like you’re making an array, so I’m pretty sure you need to use ipairs.
This won’t fix your main problem, but hopefully someone with more experience can come help you out.

no,. ipairs is not good idea for my table.

im very confused with the logic of the thing you wrote…

so im guessing

houseZones = {
[UserId] = {houseZone, anumbervaluehere}; --Right?
...
}

if this is the case then… houseZones[2] will return nil? (unless someone with UserId of 2 joins)

game.Players.PlayerRemoving:Connect(function(player)
	if houseZones[player.UserId] then
		houseZones[player.UserId][1].Parent:Destroy()
		houseZones[player.UserId] = nil --empty it i guess?
	end
end)

wouldnt this just work?

man i know about i need to remove this from table BUT i need get all houses what player bought
[UserId] = {houseZone, anumbervaluehere}; --Ye this is number value, and all variables have other values, its impossible to make like 2 houses with id 1

i feel like this wouldn’t work?

cause if a dictionary has two values with same keys wouldnt it overwrite them? so it would only have [UserId] = {houseZone,2} ?
if not then would you mind sharing how you would insert the tables?

why not just have stuff like this instead?

Table = {
  [UserId] = {
    {houseZone, 1},
    {houseZone, 2}
  };
};

what it will change with my loop? i mean if i do that it will not change anything and wont fix my loop

well this way userid has a list of housezones you can easily track them down?

game.Players.PlayerRemoving:Connect(function(player)
	if Table[player.UserId] then
  		for _, Zone in pairs(Table[player.UserId]) do
		    Zone[1].Parent:Destroy(); --also why .Parent?
  		end
		Table[player.UserId] = nil;
	end
end)

like i said im still having hard time with how the houseZones table looks like if player has multiple zones. youd need to show me how they are added to the houseZones table

houseZones[player.UserId] = {houseZone,houseNumber}

so like i said if this is true then when you already have

houseZones[player.UserId] = {houseZone1, 1}

and you are adding another houseZone it’s going to overwrite it so you wont be able to keep track of it no more…

houseZones[player.UserId] = {houseZone2,2} --the first {houseZone1,1} will be lost

so using the feature i mentioned above, the new way of inserting would be

local function InsertZone(playerUserId, houseZone, houseNumber)
    local ExistingZone = houseZones[player.UserId];
    if not ExistingZone then
       ExistingZone = {};
    end
    ExistingZone[#ExistingZone + 1] = {houseZone, houseNumber};
    houseZones[player.UserId] = ExistingZone;
end

make sure to change the Table to houseZones i guess?

i wont lost first housezone cause players are able to have more than 1 house

the object of the first housezone won’t be lost (obviously), however the stored value inside the table will overwritten. so when you do the following operations

houseZones[player.UserId] = {houseZone1, 1}

then

houseZones[player.UserId] = {houseZone2, 2}

and then call

houseZones[player.UserId]

you will only have

{houseZone2, 2}

due to the nature of the dictionary where same keys with diff values will just overwrite the value

obviously the houseZone inside workspace would still remain, just not inside the variable
since its not inside the variable you can’t use

this since it only contains one instance of the houseZone2 now.
does this make more sense?

it only contains one instance of the houseZone cause variable name is [UserId] its like when i do

local z = 1
local z = 2
print(z)

then i will get one of the variable not 2, loop wont help

i know about that thats why im asking how to do it