Any way to share same table between script?

Hi,

I’m looking for a way to share a table between two scripts. What I want is that when I change the value of one offset of the table in script one, the modification is also made on the script two (like C pointer).

I tried a lot of things but nothing seems to work with that but i think there is something i missed. So if you know a way to do that, feel free to tell it to me.

Thanks.

Send the table to another resource using Events.

Listening
Triggering

On your destination make a k,v loop to iterate through the table to see if it’s been sent correctly.

Forgot to tell that i know how to share it. But when i modify the offset on script 1, the modification isn’t made on script 2. Since i’m writting a resource that handle player data, the problem is that the data isn’t synchronized between the main script and the others scripts

I found out that when i’m sharing it, i get a copy of the table and not a ‘pointer’ of that table. And i don’t even know if its possible

Trigger the event in which you send the table data at the end of your function while having the table defined above the Event.

local table = {'YourDataHere'}
TriggerClientEvent('event', table)
-- orTriggerEvent('event',table) , not sure which one works client<->client

To access the information in your table, depending on how its structured you will need to use loops.

for k,v in ipairs(tableName) do
 -- do stuff
end

Thats what i’m currently doing but server side:

Script 1:

AddEventHandler('fetchuserdata', function(cb)
    cb(userdata)
end)

Script 2:

TriggerEvent('fetchuserdata', function(data)
    userdata = data
end)

But then, if I modify a member of the table on script 1, it won’t be modified on script 2 because they don’t share the reference.

For exemple:
table script1: 0000019B3650
table script2: 0000019B3FD0

And that is my issue, because i cannot find a way to make them have the same reference

Nevermind, I found out that if I pass function from script 1 to script 2, I can handle the player date directly on script1 from 2. I’m just dumb af. But if anyone know a way to do what I was talking previously, I’ll take it. Anyway thank you bud, you made me think and you are part of the resolution of my problem :slight_smile:

Hey man, glad you got it sorted!
Could you perhaps share how your table looks like?
I would try to get the values using a loop and compare them, if that works I’d share the code here.

I’m pretty sure the direct modification will only persist in the resource you modify it in but if you instead use functions to Get and Set then it will be readable from other resources without having to request and declare the table again.

Try Something like this:

CreateSharedTable = function()
    local self = {}
    self.Get = function(key)
        return self[key]
    end
    self.Set = function(key, value)
        self[key] = value
        return self[key]
    end
    return self
end

SharedTable = CreateSharedTable()

AddEventHandler('getSharedTable', function(callback)
    callback(SharedTable)
end)

Then in your resources that TriggerEvent to get and use the table you should be able to use the Get and Set functions and have the changes recognized across the other resources.

SharedTable = nil
TriggerEvent('getSharedTable', function(shared) SharedTable = shared end)
SharedTable.Set('SomeKey', 'SomeValue')

You should then be able to print(SharedTable.SomeKey) or print(SharedTable.Get(‘SomeKey’) from the source resource and get SomeValue back as the result which was set from the other resource.