Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't the game recognize these two values as the same?

Asked by
Trew86 175
5 years ago

Hi, I am working on a fixed block detection system for a railroad and I am having trouble detecting when a train has moved from one block to another. In a module script, I have tables for each block in the rail network.

Example of how my tables are set up in the module script:

module.Block2 = {
endPoints = {
A = {
[1] = {
Sensor = sensors.X2,
Signal = signals.S2B
}
},
B = { 
[1] = {   
Sensor = sensors.X1A,   
Signal = signals.S1A   
},   
[2] = {   
Sensor = sensors.X1B,
Signal = signals.S1B   
}    
}
}
}

Tables A and B represent the two sides of the block, and the numbered indices for each table represents the individual entry/exit points on that side.

How I defined which blocks are adjacent to one another (hopefully comments make it clear enough)

for key, block in pairs(module) do --go thru each block
block.Train = Instance.new("ObjectValue") 
--create a value that listens for changes
for k, endPoint in pairs(block.endPoints) do 
--go thru the A and B tables
for i, value in ipairs(endPoint) do
--go thru the numbered track points
for q, Block in pairs(module) do
--for each track point, go thru the blocks again
if Block ~= block then
--make sure the next block is not the same as the original block
for t, Endpoint in pairs(Block.endPoints) do
--go thru the block's A and B tables(again)
for x, point in ipairs(Endpoint) do
--go thru the table's numbered track points(again)
if point.Sensor == value.Sensor then
--if the original block shares a sensor with the new block,
value.Adjacent = Block
--then set the original track point's "Adjacent" value to the new block
print((key) .. " is adjacent to " .. (q))
end
end
end
end
end
end
end
end

And lastly, in a server script, I make every sensor in every endPoint of every block respond to a passing train. If the block is already occupied, activate the train's emergency brake. Otherwise, set the block's Train objectValue to the train. Also, look at the opposite endPoint, go through its individual points within, and find which adjacent block the train had came from.

for key, block in pairs(dataMod) do
for k, v in pairs(block.endPoints) do
for i, val in ipairs(v) do 
val.Sensor.Touched:Connect(function(hit)
if hit.Name ~= "SignalInteractor" then return end
local train = hit:FindFirstAncestor("TrainModel")
if train == nil then return end
print(train)
if block.Train.Value ~= nil then
if block.Train.Value ~= train then  functions.Emergency(train) end
else
block.Train.Value = train
print((key) .. ".Train.Value = " .. (train.Name))
for x, y in pairs(block.endPoints) do
if y ~= v then
for a, d in ipairs(y) do
local adjacentBlock = d.Adjacent
if adjacentBlock.Train == train then
adjacentBlock.Train.Value = nil; 
end
end
end
end
end
end)
end
end
block.Train.Changed:Connect(function()
if block.Train.Value ~= nil then
print((key) .. " entered")
functions.protectBlock(block)
else
print((key) .. " exited") 
functions.openBlock(block)
end
end)
end

I've been stuck on this all day. I think the problem is that the game is not recognizing when the Train value of the entered block equals the Train value of the previous block. Output prints the adjacent blocks correctly, along with the train entering each block. Just doesn't open the block back up.

0
My eyes have burned when i saw that code it isn't neat lmao Oskar2266001 3 — 5y
0
i'm sorry Trew86 175 — 5y

Answer this question