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.
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
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
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.
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.