Swapping items of data in array to different positions

This is gonna kinda tricky to explain, but I’ll try. So I’ve found a problem with my inventory where when I update it, any movements I make from the client get reverted (you can drag your items around the inventory to different slots, but I never connected those changes to the server)

So here, I move 2 items into a stored slot, then use one of my axes until it breaks. When it breaks I fire an update to the client, which is the players inventory, and so when they client gets their inventory from server, there items are still in their original spots.

Hope this gif gives the general idea :grimacing:
robloxapp-20191229-1907347

So, as you can see, my two axes which I moved, go put back into their original slots, so I need to do some client-server stuff to change them in the server.

What I’ve got

-- Client
local OriginalFrame = v.Parent
v.Parent = frame.Parent
frame.Parent = OriginalFrame

local SlotSection1
local SlotSection2
if OriginalFrame.Name == 'MainSlots' then
	SlotSection1 = {
		Section = 'Main', 
		Number = OriginalFrame.Parent.Name
	}
else
	SlotSection1 = {
		Section = 'Stored', 
		Number = OriginalFrame.Parent.Name
    }
end
									
if frame.Name == 'MainSlots' then
	SlotSection2 = {
		Section = 'Main', 
		Number = frame.Parent.Name
	}
else
	SlotSection2 = {
		Section = 'Stored', 
		Number = frame.Parent.Name
	}
end

ItemMoved:FireServer(SlotSection1, SlotSection2)

So this basically occurs when the item has been moved. So my logic was OriginalFrame is the item you are moving, then ‘frame’ is where you are moving it toooo. So it should look like this. if I move item in Slot 1 (main) to Slot 3 (stored)

SlotSection1 = {Section = 'Stored', Number = '3'}
SlotSection2 = {Section = 'Main', Number = '1'}

Then server would go

local function MovedItem(player, slotSelection1, slotSelection2)
	local User = PlayerData[player.UserId]
	if not User then return end
	
	local OriginalData = User.Inventory[slotSelection1.Section][slotSelection1.Number]
	User.Inventory[slotSelection1.Section][slotSelection1.Number] = User.Inventory[slotSelection2.Section][slotSelection2.Number]
	User.Inventory[slotSelection2.Section][slotSelection2.Number] = OriginalData
end

And so like the item in their slot 1 main inventory would be moved over to the Slot 3 stored inventory and vice versa

-- Inventory layout

Inventory = {
		Main = {
			[1] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 30},
			[2] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 30},
			[3] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 5},
			[4] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 10},
			[5] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 20}
		},
		Stored = {
			[1] = {},
			[2] = {},
			[3] = {},
			[4] = {},
			[5] = {},
			[6] = {},
			[7] = {},
			[8] = {},
			[9] = {},
			[10] = {}		
		}
	}

TL;DR basically, when I move my inventory around, I want to communicate with the server and corrospond the moved items to the server, so the server can update my inventory

2 Likes

What do you mean here?

You already have the remote event communicating with the server. I don’t see a problem here. Can you post the code where you update the inventory when the ax breaks? Maybe the problem happened there. And, is your players’ inventory stored in a local or server script?

(Edit: Oh wait I just figured that out. In your server script you said PlayerData. So you stored it in the server?)

1 Like

Yes, Inventory is handled/stored on the server. I send a copy over to the client so I can set up their invenotyr etc. but any action they do (equipping/unequipping, etc.) gets communicated back to the server, however, moving items doesn’t (well that’s what I’m trying to add atleast) so moving them around on the client works, but when your inventory gets updated by the server (you lose an item, or whatever) then you go back to your original layout, as the server doesn’t know you’ve moved the items around

1 Like

Your MovedItem() function makes sense, have you yet debugged through printing the old and new values?

1 Like

Have you tried putting a number datatype for the slot instead of a string? It’s likely they are referencing different things. If this is true, you should see if wrapping the slot number with tonumber changes anything.
i.e:

-- before moving data
Inventory = {
    Main = {
        [1] = { --[[axe data]] },
        [2] = { --[[more axe data]] },
        --...
    },
    -- ...
}
-- after moving data
Inventory = {
    Main = {
        [1] = { --[[axe data]] },
        ['1'] = {},
        [2] = { --[[more axe data]] }
        -- ...
    },
    Stored = {
        -- ...
        [3] = {},
        ['3'] = { --[[axe data]] },
        [4] = {},
        -- ...
    },
}

You would just send data like normal and change it on the server:

local function MovedItem(player, slotSelection1, slotSelection2)
	local User = PlayerData[player.UserId]
	if not User then return end
	
	local sect1, num1 = slotSelection1.Section, tonumber(slotSelection1.Number)
	local sect2, num2 = slotSelection2.Section, tonumber(slotSelection2.Number)
	
	-- x, y = y, x
	User.Inventory[sect1][num1], User.Inventory[sect2][num2] = User.Inventory[sect2][num2], User.Inventory[sect1][num1]
end
1 Like