How to move an object in a table?

Hello, I’m trying to move the position of an object in a table to a different index in the table

Is there a way that I can move the position of a single object and the table automatically manages the moved index

Here is what it would look like

image

6 Likes
function switchTableValue(table, firstKey, secondKey)
   local switchVar = table[firstKey]
   table[firstKey] = table[secondKey]
   table[secondKey] = switchVar
return table
2 Likes

I am not replacing them to each other, I am moving the position of a Single one

2 Likes

Just curious is this for an inventory slot system

1 Like

What do you mean?

I am not replacing them to each other

is the same thing as “I am not moving them”

I am moving the position of a Single one

Yes, but doing that would leave a duplicate.

1 Like

image

This is what I mean

1 Like

Well I did that.
Do you mean copying?

1 Like

This is mine
image

This is yours
image

1 Like

Are you joking or did you select the wrong image?
Both are the same.

1 Like
local index = -- index of the item
local item = table.remove(t, index)
table.insert(t, newIndex, item)

Is this what you mean?

2 Likes

You are a troll bro. I’m reporting you. Use your eyes to observe both images are different.

2 Likes

They’re different. But they resolve to the same thing. How are you not banned from the DevForum already? I saw you spamming a bunch of obvious/non-sense topics a while ago.

Your incessant disruption is, well, incessant. @msix29 has provided the correct solution. Your antics are finally over. People have reasons for doing things specific ways. Just because you are ignorant in coding doesnt mean I have to change my style. I suggest you educate yourself more and stop trying to solve devforum scripting problems when you yourself need help.

local index = -- index of the item
local item = table.remove(t, index)
table.insert(t, newIndex, item)

is the same thing as

function switchTableValue(table, firstKey, secondKey)
   local switchVar = table[firstKey]
   table[firstKey] = table[secondKey]
   table[secondKey] = switchVar
return table

Why did you suddendly start to accuse me of being a troll out of nowhere? And why did your english suddenly get good?

This is ridiculous and I won’t answer to you anymore. Goodbye

This is theirs
image

This is yours
image

there is clearly a difference. Theirs is more aesthetically pleasing and also what I asked for

1 Like

@KrimsonWoIf and @GenericsAccount. Guys, chill out, GenericsAccount didn’t understand and that’s completely fine, yes his replies came off with attitude but a mature and good response would be to completely ignore such things or explain. For Generics, the 2 images are different (look at bottom row) but I will say, the images are actually wrong. You can’t blame Krimson though, it isn’t easy to visualize such thing.


They are not, yours switches the value at the first key with that of the second key while mine moves the item at a key to another key while keeping the item at that other key in the table.


Make my post as a solution for future comers. :+1:

1 Like

I… know? That’s my whole point…

Oh okay! I didn’t know he meant copying the value.
Goodbye

Glad this argument settled. Ba bye.

1 Like

no, I’m not copying the value, I’m moving a single value to the other value position. I am not swapping them either, I am merely moving a singular one.