Are tables client side or server side?

So lets say I have a table:

local myTable = {1,2,3,4,5}

And I inserted something into said table

table.insert(myTable,6)

Would this change affect the tables of all players? If so, is there a way I can make it only affect a specific player?

1 Like

Not sure what you mean there, you need to specify, what you are trying to achieve and as always, provide your script.

Depends if every player has their own server script which means table will only change on their server script or are dependent on one server script.

Tables are within both server and client.

If you create a table on the client, it stays on that client.

If you create a table on the server, it stays on the server.

There is no sharing of tables over the network (so a client cannot access a table on the server and vice versa).

I don’t have a script yet, but I have a basic concept worked out in my head
So an example would be perhaps a trophy system, and if you get a trophy it’s inserted into the table. Obviously, you’d want this to be independent for each player. Something like this:

local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.event
local trophies = {
    trophy1;
}

event.Event:Connect(function()
      table.insert(trophies,trophy2)
end)

So my question is just would this be stored server side or client side, considering this was coded in a server script, and would the change affect all tables or just one

Then create that table inside a LocalScript. If you want to display how many trophies that player has to others, you can just send the table to the server using RemoteEvent

Things that are created inside the LocalScript will be specific to that player only. Any changes happens to that table will be seen by only that player. It is like bags of chips. Many have the same bag of chips, but the amount of chips will be independent to the one who owns it. If that would make it easier to understand, that would be great…

I don’t know what you mean by all tables but it will affect the table thats within the script

That makes sense. What if player was added as an argument in the function where the trophy is added?

You would want to send the table instead of the player in case that you save trophy data inside the table. Unless you save it as StringValue or BooleanValue in some Folder then you can send the player to let the server checks player’s data from there.

Also, if you use RemoteEvent sending from client to server, the First arguement of OnServerEvent will always be the player who fires that RemoteEvent anyways, but in this case, you also would want to send the table as a second arguments like this

-- CLIENT PART
local TPTable = {}

TrophyRemote:FireServer(TPTable)


--SERVER PART
TrophyRemote.OnServerEvent:Connect(function(player, trophyTable)
     -- Do things.
end)
1 Like

Wouldn’t this leave a major vulnerability for exploiters, though?

That is why checking on server-side is important, and can come into play. First thing first before I say anything else, do not be afraid on using RemoteEvent. I can ensure that it is essential in making your game. You can further verify if the player actually have those trophies inside their profile, their save or whatever, if not and you are sure that they don’t have those trophies, kick them out! Server-side Validation is really important to protect your game from exploiters.

1 Like

For a more secure method,
in a server script in ServerScriptService, create a dictionary.
When a player joins, add a key-value pair to the dictionary with the key being the player’s name and the value being the table.
Since everything is on the server side, theoretically, exploiters won’t be able to mess with anything.
You now have a table for each player on the server.

1 Like

This is perfect, I was very hesitant to use a local script for sending the table. Question, would they automatically delete after a player left? Would it have to be manually saved somehow

1 Like

Depends, it is based on how you implemented it. If you recreate a new table from nothing when a player logs in, that table won’t be persistent. If you save that table using DataStore, it will be persistent.

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