Workarounds / Solutions for Sending an Array from the Client to the Server?

In my game, I am trying to implement a system where you sacrifice items in your inventory to gain a permanent boost. To do this, the player selects the items in their inventory they want to sacrifice. This leaves them with an index of items that they are sacrificing akin to this:

{
                    [1] = 1, -- index means nothing, value is the id of the item being sacrificed
                    [2] = 2,
                    [3] = 3, --  so on and so forth
                 }

This array, created by a local GUI script, is supposed to be sent using FireServer to a server-sided module script that removes the items within the player’s inventory and updates a boost accordingly. (indexToBurn is the sent index)

torchEvent.OnServerEvent:Connect(function(player, indexToBurn)
	local totalWorksBurning = 0
	print(player.Name, indexToBurn)
	
	for index, value in ipairs(indexToBurn) do

However, the function does not receive the index and instead, indexToBurn is an “Instance” that the code prints as nil. From what I read this is because the server script does not have access to the index within the local GUI script, so the server script cannot read it.

ServerScriptService.PlayerWorkIndex:55: invalid argument #1 to ‘ipairs’ (table expected, got Instance)
(Switching ipairs to pairs has the same result.)

My issue is that I still need to have the index accessible to the server script. Is there any alternative way for me to accomplish this, or am I possibly misinterpreting how FireServer works?

1 Like

When you send the array it should look something like this:

-- On the client side:

local array = {"Hello", "world", 42}

-- Use the 'FireServer' function to send the array to the server.
game.ReplicatedStorage.SomeEvent:FireServer(array)


This code was AI generated from: https://chat.openai.com/chat

This is the code where I send the array. I also have to send the player, and the player sends without issue.

torchEvent:FireServer(player, worksBurningIndex)

Is there something else I am missing? worksBurningIndex prints without issue on the client.

You don’t have to send the player through the FireServer() event.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.