dataMod = require(game.ReplicatedStorage.Modules.RailwayData) functions = require(game.ReplicatedStorage.Modules.RailwayFunctions) for k, block in pairs(dataMod) do for key, endPoint in pairs(block.endPoints) do for i, trackPoint in ipairs(endPoint) do trackPoint.Sensor.Touched:Connect(function(hit) if hit.Name ~= "SignalInteractor" then return end local train = hit:FindFirstAncestor("TrainModel") if train == nil then return end if block.Train then if train ~= block.Train then functions.Emergency(train) end else block.Train = train --this is not working. functions.protectBlock(block) for j, Endpoint in pairs(block.endPoints) do if Endpoint ~= endPoint then for t, Trackpoint in ipairs(Endpoint) do if Trackpoint.Adjacent.Train == train then Trackpoint.Adjacent.Train = nil; functions.openBlock(Trackpoint.Adjacent) end end end end end end) end end end
Before you mention the huge amount of nested for loops, please be advised that my only problem here is at the line with the comment next to it. When the train's Signal Interactor part touches the sensor of the block (which is a table from a module script), I want the game to save the train as a value in the block(table). After that line, the protectBlock function runs successfully, yet when I used the command bar to print the train value that should be in the block, I get "nil" in the output.
I'm truly stumped, because one line doesn't seem to be working, but the one after it does and there are no errors in the output.
Here is an example of how I have the block tables set up (in dataMod):
module.Block4 = { endPoints = { A = { [1] = { Sensor = sensors.X3, Signal = signals.S3A } }, B = { [1] = { Sensor = sensors.X4A, Signal = signals.S4A }, [2] = { Sensor = sensors.X4B, Signal = signals.S4B } } } }
And here is how I defined the adjacent blocks (in dataMod):
for key, block in pairs(module) do for k, endPoint in pairs(block.endPoints) do for i, trackPoint in ipairs(endPoint) do for j, Block in pairs(module) do if Block ~= block then for q, Endpoint in pairs(Block.endPoints) do for r, Trackpoint in ipairs(Endpoint) do if Trackpoint.Sensor == trackPoint.Sensor then trackPoint.Adjacent = Block end end end end end end end end
and finally, here is the function that isn't getting to be called (in functions):
module.openBlock = function(block) for key, endPoint in pairs(block.endPoints) do for i, trackPoint in ipairs(endPoint) do if trackPoint.Adjacent.Train then for k, Endpoint in pairs(block.endPoints) do if Endpoint ~= endPoint then for j, Trackpoint in ipairs(Endpoint) do signalMod.yellowLight(Trackpoint.Signal) end end end else for k, Endpoint in pairs(block.endPoints) do if Endpoint ~= endPoint then for j, Trackpoint in ipairs(Endpoint) do signalMod.greenLight(Trackpoint.Signal) end end end end end end end
I'm almost certain my problem is in the first script, because it stops working somewhere between the functions.protectBlock line and the functions.openBlock line.