Why does this table come back after making it nill?

I’ve been trying for about 1 hour to figure out why this table just comes back after printing it in another for pairs or using it works, when its already nill
i dont know why it does this, i also tried just not using the table anywhere outside of the prints

this is the code:

Players.PlayerRemoving:Connect(function(player)

	for k, v in pairs(enemies) do
		print(k,v)
		
		for k2, v2 in pairs(v) do
			if k2 == player.UserId then
				print(k,v)
				print(k2)
				print(v2)
				print("yo someone left")
				v2 = nil
				k2 = nil
				v = nil
			end
			print(k,v)
		end
		print(k,v)
	end
	
	for k, v in pairs(enemies) do
		print(k,v)
	end
	
end)

image

1 Like

Generally, this is how you should remove values from a dictionary:

dict[key] = nil --where dict[key] used to have a value

so for your use case, this should do:

v[k2] = nil

and since you set v to nil just do:

enemies[k] = nil 

Pretty sure garbage collection can handle the rest.

2 Likes

Thank you, this worked! I guess I should do that from now on

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