Tables Between Scripts

Hello, I have a few situations in my game where I need a table to be accessible and editable from multiple scripts, however, there’s no tableValue so I’ve attempted to use moduleScripts. But I’ve had trouble when it comes to editing a table from a moduleScript. It seems to set it from one script but then from a seperate localscript it apparently didn’t update, so I’m coming here to see if anyone else had had a similar issue and solution.

(And, for context, I’m trying to get editable tables for map voting. I want to add everyone’s vote to a table, then go through it to see which number occurs the most. Another table that needs to be constantly edited is a table that dictates which maps are available for voting)

Thank you and good day!

Try using a StringValue:
Convert the table into a JSON string, and put them in the StringValue

As you tried, using Modules is a good idea. Perhaps the issue you experienced is about context of the scripts that tries to edit the Module.

If the Module belongs to Server a LocalScript wont be able to require it or edit it.
If Module is accessable by clients, the changes on that module will only reflect to each client, not to others.

So you cant have a module that be editable by many different clients and then able to be edited by the server. All are different context.

But, why dont you try to let players to vote and by using a Remote you collect their votings, store them in a table and then you decide the most voted map?

You can use _G or the SharedTableRegistry service.

_G can be accessed ONLY by their script classes. E.g. LocalScript Shared tables can only be accessed by other LocalScripts and nowhere else. This might not help with the problem you’re having.

As with SharedTableRegistry, there’s not a ton of information about it that could give me a way to simplify and provide instructions for it, but you should go check out the documentation on it.

But if a LocalScript creates a SharedTable, that table only exist for that Client, the Server and other clients cant check it or edit it. How to make a voting system if nothing is connected at all using that approach?

Thank you guys for giving me these solutions. I’m going to try them and come back when I have got it working!

Exactly what I was going to say. Building on this, it should be similar to:

local stringValue = path.to.your.value.object
local httpService = game:GetService("HTTPService")

local function vote(player, vote)
    local currentVotes = httpService:JSONDecode(stringValue.Value)
    currentVotes[player] = vote
    stringValue.Value = httpService:JSONEncode(currentVotes)
end

game:GetService("ReplicatedStorage").VoteEvent.OnServerEvent:Connect(vote)

Tried something like this, and it works!

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