Need help connecting a Model to a value in a table in a ModuleScript within ReplicatedStorage

This is a bit of a mouthful to explain.

I’m developing a scale replica of the Ignalina Nuclear Power Plant in Roblox, where players will have to correctly operate the plant to generate power and earn money for doing so.

There are hundreds of fuctional switches (thousands counting the non-functional ones, yes I am aware this is a lot to handle rendering) that will correspond to some value, such as a water valve or electrical breaker and stuff like that. These values are held in a ModuleScript located in ReplicatedStorage titled ReactorData; it holds all the data that can be set and read by the game.

What I am trying to achieve is to have some way to “connect” a switch to a value from this table. I want the server to, upon receiving a message that switch X has been pressed, and modify switch X’s corresponding value. I have no idea how to do this; I’ve tried putting a module in switches that returns a value from ReactorData, but upon testing this did not work and wouldn’t actually set values.

This might be easier to explain visually; here’s a basic MS Paint diagram of how things will work.

Any help would be greatly appreciated! If I’m not very clear please feel free to ask me for more details on something.

1 Like

P.S. I have to leave for a while but I will respond to any replies as soon as I get back home.

Assign each switch an attribute corresponding to the reactor data key that it modifies and then just change the value of that key in the reactor data after the switch sends a signal. Below I have included pseudo-code explaining how to do this. The code below is not intended to be the solution, but rather an example of how to go about doing this.

local switch = script.Parent

switchEvent:FireServer(switch:GetAttribute("SwitchType"), true)
switchEvent.OnServerEvent:Connect(function(player, targetReactorDataKey, changeValueTo)
    reactorData[targetReactorDataKey] = changeValueTo
end)

You could use metatables, Although as you mentioned you will want this to be replicated therefore this won’t be super viable.

Here is some pseudo-code

local reactorData = {
	Meta = {},
	Proxy = {}
}

reactorData.Meta.__index = reactorData.Proxy
reactorData.Meta.__newindex = function(_tabl, index, value)
	if (_tabl[index] ~= value) then
		reactorData.Proxy[index] = value
	
		print(`value with index {index} changed to {value}`)
	end
end

setmetatable(reactorData, reactorData.Meta)