Table Not Removing From Dictionary

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am making a tower defense game, recently I switched from keeping towers in tables and moved them to arrays allowing me to add things like skins.
For example:
Before - SelectedTowers = {“Scout”}
Now -

SelectedTowers = {
 ["Scout"] = {
   Name = "Scout",
   Skin = "Default",
   OwnedSkins = "Default",
   Perk = "None"}

}

  1. What is the issue? Include screenshots / videos if possible!


Everything works except removing towers from the dictionary. The image isn’t actually from my game but a simple recreations. I set the tower to nil hoping to remove it but after printing it stays there. Thus I can’t unequip towers since it stays in the dictionary.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried table.remove but it doesn’t work with arrays. Other people who have similar questions ask about table.remove but I’m not using it.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Did you try towers[“Scout”] = nil?

Since I cant use table.find for a dictionary, I instead loop through the entire dictionary and if the Tower is found I delete it. A Although towers[“Scout”] = nil does work it doesn’t work in the for loop.

Okay I realized the problem but now I have 2 more stemming from that. “dictionary” in the for loop has the value of its contents and not well the actual table. E.g instead of Scout it would be the Skin and Perk etc when I need the table itself. Also when I try to delete the contents of the table it isn’t removed for some reason either.

1 Like

Can you send your for loop that you use?

for _, dictionary in pairs(playerData.SelectedTowers) do
	if dictionary.Name == itemName then
		return "Equipped"
	end
end

robloxapp-20240803-1605041.wmv (153.4 KB)
It used to remove it from the table changing the text to unequip but as seen in the logs the table stays there.

the “_” that you set is actually the key (Or the tower’s name). When you want to delete the table from it, you can do playerData.SelectedTowers[key] = nil

The video doesn’t show anything code, just the buildings

Yo thanks so much I did NOT understand that ty ima go fix it now

1 Like

i mean showing the buttons when they are activated they click and the output posts the new dictionary the buttons removed the towers from the table but this time they dont

1 Like

IT WORKS finally now i just gotta add skincrates