Trade System Array Passing

I have been struggling on why my arrays aren’t updating to the value assigned by the Remote Event. I have a Server Script pass an array copy to the client that already has variables ready to copy the contents of the server’s array. My code is below. Whenever it runs, I get the value of senderHats is 0, so I know that the value isn’t being passed. Help is greatly appreciated!

CLIENT

--[[RECEIVER]]--

--[[Services]]--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--[[Remote Events]]--
local receiveTradeEvent = ReplicatedStorage.RemoteEvents:WaitForChild("ReceiveTradeEvent")

--[[Variables]]--
local button = script.Parent
local opened = false
local player = Players.LocalPlayer
local tradeRequestWindow = player.PlayerGui:WaitForChild("TradeGui").TradeMenu.TradeRequest
local updateHats = false

--[[Hats requested from player]]--
local senderHats = {}

--[[Hats the player is willing to give]]--
local receiverHats = {}

-- Upload the hats in the trade to the trade request window --
button.MouseButton1Up:Connect(function()
	print(#senderHats)
	for i = 1, #senderHats do
		senderHats[i].Parent = tradeRequestWindow.ReceivedHats
		print(senderHats[i].Name)
	end
	
	for i = 1, #receiverHats do
		receiverHats[i].Parent = tradeRequestWindow.RequestedHats
		print(receiverHats[i].Name)
	end
	
	tradeRequestWindow.Visible = true
	
	-- Remove the trade request when opened to prevent complexities --
	button:Destroy()
end)

receiveTradeEvent.OnClientEvent:Connect(function(sHats, rHats)
	for i = 1, #sHats do
		table.insert(senderHats, sHats[i])
	end
	for i = 1, #rHats do
		table.insert(receiverHats, rHats[i])
	end
end)
1 Like

Where exactly is senderHats and receiverHats updated? I don’t see it.

EDIT: I see it now. Are you sure sHats and rHats are arrays and not tables (aka they use continuous numbrrs as keys instead of strings)?

receiveTradeEvent.OnClientEvent:Connect(function(sHats, rHats)
	print(#rHats)
	for i = 1, #sHats do
		table.insert(senderHats, sHats[i])
	end
	for i = 1, #rHats do
		table.insert(receiverHats, rHats[i])
	end
end)

To make it easier to debug why don’t you print what sHats and rHats contain like you do in the other function

print(rHats[i].Name)
print(sHats[i].Name)

The amount of items inside the sHats and rHats is 0 on the client, but from the server I do this:

local senderGivenHatsCopy = {unpack(playerHats)}
	local senderRequestedHatsCopy = {unpack(targetHats)}
	local receiverGivenHatsCopy = {unpack(playerHats)}
	local receiverRequestedHatsCopy = {unpack(targetHats)}
	
	print(#receiverGivenHatsCopy)
	sentTradeEvent:FireClient(player, senderGivenHatsCopy, senderRequestedHatsCopy)
	receiveTradeEvent:FireClient(Players[targetName], receiverGivenHatsCopy, receiverRequestedHatsCopy)

There the output gives me a nonzero number.

Just for anyone is experiencing the same problem, you cannot pass objects with remote events. You need to load those objects from the server directly onto the player, not send the objects to the player.