A value is removed from a table if it appears with the same name as another

Hey, there! I have made a table that basically stores the player’s ID and message. Sadly, if two messages with the same ID are found, the table removes one! What can be done?

Here’s an example code:

local GeneralChats = { 
	[122227420] = "Hey there.", --This one get's deleted automatically
    [122227420] = "Another message from me!",
	[122227425] = "Adopt me is casino for kids",
}

You could check generalChats[idYouGeneratedHere] and if it prints nil then you just add it

The problem is that the chat system I’m making needs to save multiple messages. When a player sends a second message, then the first message gets deleted, which I don’t want!

What? That isn’t even possible. You just said if 2 ID’s matched, one would be deleted. Do this

generatedId = math.random(1, 100000) --you can do whatever you want

while GeneralChats[generatedId] do
     generatedId = math.random(1, 100000)
end

--then just do other stuff

It should not be random numbers, but the player’s ID.

Since you try to assign a new value to the same key in the table, the new value will overwrite the old value. Instead of that, have you considered assigning an array to the player’s user id instead?
Something like this:

local GeneralChats = {
   [122227420] = {
      "Hey there.",
      "Another message from me!",
   },

   [122227425] = {
      "Adopt me is casino for kids",
   }
}

In this case, you would append new messages to the end of the array and the messages will stay in the order they were added.

What a brilliant idea, thanks! I had never considered it.

1 Like