How to update my SharedTable for other scripts?

Hi! I’m currently trying to use parallel lua which I’m completely new to.

I have a ton of different actors with scripts in them, and all the scripts are updating 1 SharedTable.


To set up my SharedTable, from my main script I have:

local SharedData do 
	SharedData = SharedTable.new({}) -- There is a table here with indexes, I just left it out to shorten this
	SharedTableRegistry:SetSharedTable("SharedData", SharedData)
end

Then, all the worker scripts they’re constantly updating indexes like so:

local SharedData = SharedTableRegistry:GetSharedTable("SharedData")

RunService.Heartbeat:ConnectParallel(function(DeltaTime : number)

	local Increment = math.random(1, 20)
	SharedTable.increment(SharedData, MyIndex, Increment)
end)

But then when you print the SharedData, the indexes aren’t being updated.

What am I doing wrong?

Are they actually the same table? Print them and see if their addresses are the same.

Hmm, after printing the SharedTable with LogMode on, I guess not? (Orange text is the main script, the white is one of the actor scripts)

image

But why not though? I don’t understand what I did wrong. If I have to share more of my code lmk

This is probably a deliberate design choice so you can pass tables to parallel functions without worrying about making a thread-local copy. The underlying call probably makes a deep copy of the table and passes it to your function. Not sure how you’d get around this though.

So what even is the point of a shared table then? From what that sounds like it means you should only be sending data from one script to a bunch of other scripts, but not the other way around (which is what I need)

Parallel sucks currently because they could not find a good locking mechanism for stuff exactly like this, that was user friendly enough. If I think of a workaround for this I’ll let you know though.

2 Likes