Parallel lua shared appears to not share across actors?

The following code prints twice:

	if not shared.lagcomps then
		print("New lag comp!")
		shared.lagcomps = {}
	end

I doubt it’s a race condition as each new actor that contains this script is added like this:

	players.PlayerAdded:Connect(function(player)
		local hitbox = script.Hitbox:Clone()
		
		hitbox.Name = player.Name
		hitbox.Parent = replicatedstorage.Hitboxes
		
		local actor = Instance.new("Actor")
		local worker = script:Clone()
		worker.Player.Value = player
		worker.Parent = actor
		workers[player] = actor
		
		actor.Name = player.Name
		actor.Parent = threads
	end)

Why is shared.lagcomps nil across the threads? Am I misunderstanding something?

I have also tried to do shared.lagcomps in the root script to no avail.

Each actor runs in its own environment and the shared table may not work as a global table across different actors, you can do this by creating a SharedTable using the SharedTableRegistry Service

local SharedTableRegistry = game:GetService("SharedTableRegistry")
local sharedData = SharedTable.new()
SharedTableRegistry:SetSharedTable("MySharedData", sharedData)

Access this shared table in your actors using the SharedTableRegistry .

local SharedTableRegistry = game:GetService("SharedTableRegistry")
local sharedData = SharedTableRegistry:GetSharedTable("MySharedData")

if not sharedData.lagcomps then
    print("New lag comp!")
    sharedData.lagcomps = {}
end
1 Like

Great, was using the wrong thing. Thought shared and SharedTable was the same thing.

Many thanks!

1 Like

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