I am coding a roblox train signalling system and I am currently at the first phase to make sure signals change state when entered. But I’m running into a problem with the debounce:
Every train has 2 sensors and thus the event fires 2 times. This is not a problem when a train is entering a block however when its exiting it triggers 2 times and makes the signal green when it should be yellow.
I hope my explanation was good enough and you can help me.
Thanks in advance!
local SIGNALS = workspace.FutureSignalling.Signals
local Sensors = workspace.FutureSignalling.Sensors
local Trains = workspace.Trains
local signals = {}
local debounce = {} --adds train to table
local greenColor = Color3.fromRGB(0, 255, 0)
local yellowColor = Color3.fromRGB(255, 255, 0)
local redColor = Color3.fromRGB(255, 0, 0)
local blackColor = Color3.fromRGB(0, 0, 0)
function SetState(signal, state)
print(signal)
print(state)
if state == 0 then --RED
signal.Green.Color = blackColor
signal.Yellow.Color = blackColor
signal.Red.Color = redColor
elseif state == 1 then --YELLOW
signal.Green.Color = blackColor
signal.Yellow.Color = yellowColor
signal.Red.Color = blackColor
elseif state == 2 then -- GREEN
signal.Green.Color = greenColor
signal.Yellow.Color = blackColor
signal.Red.Color = blackColor
else
error("Signal does not have a state")
end
end
function BlockEnter(signal,otherpart)
local data = signals[signal]
if table.find(debounce, otherpart:FindFirstAncestorOfClass("Model").Parent.Parent) then return end
print("ya pas de debounce")
if otherpart.Name == "Sensor" then
data.state = 0
table.insert(debounce,otherpart:FindFirstAncestorOfClass("Model").Parent.Parent)
data.in_block = debounce
end
SetState(signal, data.state)
print(data.in_block)
debounce = {}
end
function BlockExit(signal,otherpart)
local data = signals[signal]
if table.find(debounce, otherpart:FindFirstAncestorOfClass("Model").Parent.Parent) then return end
print("ya pas de debounce")
if otherpart.Name == "Sensor" then
data.state += 1
table.insert(debounce,otherpart:FindFirstAncestorOfClass("Model").Parent.Parent)
data.in_block = {}
end
SetState(signal, data.state)
print(data.in_block)
debounce = {}
end
for _, child in SIGNALS:GetChildren() do
local data = child:FindFirstChild("SignalData")
if data and data:IsA("Folder") then
--adds the signal to table
signals[child] = {
[ 'state' ] = data:FindFirstChild("State").Value and data.State.Value,
[ 'enter_points' ] = {},
[ 'exit_points' ] = {},
[ 'in_block' ] = {}
}
end
for _,v in data:GetChildren() do
if v:IsA("ObjectValue") and v.Name == "EnterPoint" then
table.insert(signals[child][ 'enter_points' ], v.Value)
elseif v:IsA("ObjectValue") and v.Name == "ExitPoint" then
table.insert(signals[child][ 'exit_points' ], v.Value)
end
end
print(signals[child].enter_points)
task.wait(2)
for _,v in signals[child].enter_points do
v.Touched:Connect(function(part)
BlockEnter(child, part)
end)
end
for _,v in signals[child].exit_points do
v.Touched:Connect(function(part)
BlockExit(child, part)
end)
end
end
this is the explorer(same thing for the other coach with a sensor)
video(notice how the signal gets triggered twice when it shouldnt)
