Accessing a global table from Client-required ModuleScript?

I am having issues figuring out how I could go about creating a table in a ModuleScript that is viewable by the client and would update for all clients after changes are made.

Here is my setup:

local global_table = {}

function GetNewValue(): number
    local new_value = 1

    if table.find(global_table, new_value) then
        task.wait()
        GetNewValue()
    else
        table.insert(global_table, new_value)
        return new_value
    end
end

function module.new()
    local self = setmetatable(module, {
        -- Creating the object blah blah
        value = GetNewValue()
    })

    return self
end

EDIT: The value is essentially going to be a “session ID” sort of variable, which means it needs to be unique and can’t exist anywhere else.

From what I know, the only method of updating something across all clients without it being on the server, you’d need to use the server to replicate it to others. So: client1 adds to table and fires a remote event → server answers remote event and fires a remote event to all clients → all clients (except client1) add the same thing to the table. This way, all the clients have it added but the server itself doesn’t store it. To have the same table be added to new clients so they’re up to date, you’d just take a player that’s already in the server (using the server) and prompt the new client to add all the values.

I wrote this badly tbh so if something doesn’t make sense I can go into more depth.

2 Likes

No I get how this would work. I was hoping for a solution that would keep this entire module a single module (not involve using scripts located in ServerScriptService, etc.) in ReplicatedStorage, however, if this is the only solution that anyone can think of then I guess it would work. Thank you!

1 Like

May I ask why you don’t want the value to be stored on the server? This way you can still have one module, it also handles the replication for you.

1 Like

The server has to play a part in replication to clients somehow. Direct client-client communication is impossible and even if it wasn’t, it would pose a lot of security risks. You should never trust the client entirely – especially when it comes to replicating information from one client to another. I feel like you’re making this a lot more complex by generating these numbers on the client, as you would have to, as it has been stated, pass the table through RemoteEvents to replicate to other clients. Not only that, you would have to verify each index inside of the table since an exploiter can modify the value however they want. You state that this value needs to be unique. If your system heavily relies on the value being unique, how can you verify that the value is indeed unique without any sort of reference on the server?

1 Like

I don’t really know why I wanted to keep everything in the module. I was going to have to connect it to the server anyway. Your solution works perfectly fine and is likely the best way to implement it in this case. Thank you!

I agree. I don’t really know why I wanted everything to be handled by each client, but I appreciate your response. It is well written and makes a lot of sense.

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