What do you want to achieve? I want to make this system work in the following way. 1) A train enters a block zone. 2) The script recognizes that the train is in a block zone, and stores that in an ObjectValue. 3) I can use the ObjectValue with Train Arrival Signs and Signal Lights.
What is the issue? My code is using the “Wheel” parts of my train, and my code kind of works, but it seems that when a train is in a signalling block, the ownership will switch on and off crazy fast.
Here’s my code:
local system = script.Parent
local blocks = system.Blocks
local lights = system.Signals
local touching = false
for i, v in pairs(blocks:GetDescendants()) do
print (i, v)
if v.Name == "blockPart" then
v.Touched:Connect(function(hit)
if touching == false then
if v.Parent.Value.Value == nil then
print("touched function begins")
task.wait(0.5)
if hit.Name == "Wheel" then
v.Parent.Value.Value = hit.Parent.Parent.Parent.Parent
print(tostring(v.Parent).." is now occupied.")
touching = true
end
end
end
end)
v.TouchEnded:Connect(function(hit)
print("TOUCH ENDED!")
if touching == true then
task.wait(0.5)
print("TOUCHING == TRUE WHILE TOUCHENDED")
if hit.Name == "Wheel" then
print("WHEEL WAS NO LONGER TOUCHED")
if #workspace:GetPartsInPart(v) == 0 then
v.Parent.Value.Value = nil
print(tostring(v.Parent).." is now unoccupied.")
touching = false
end
end
end
end)
end
end
What solutions have you tried so far? I looked on the DevForum and DevHub as well as did lots of experimenting with different systems and functions, but nothing worked.
This system is based off of the NYC Subway’s Block signaling system.
For more information about NYC Subway Block signaling:
This is going to be a difficult and inconsistent way to do this. I would recommend a different system that uses invisible parts to define each block then checks a list of trains for which ones are in each block.
.Touched and .TouchEnded are fairly inconsistent in their firing. There is a system that can detect if parts are in a certain area that I think would be better suited for this: See Methods Section:
Here is an example of how you could do it. The red blocks are train locations, the gray parts would be your invisible markers for blocks. These change color for caution and danger respectively if you move the trains around. Right now you have to manually specify which blocks are adjacent to eachother (see the script) but you could come up with a way to automate this part if you have lots of tracks.
This looks like a great system and thanks for making this just for an explanation. However, I really do not understand how the script works, and if I want to manipulate it so I can do other things with the blocks (like making Next Train to Arrive indicators,) I need to know how this works. Can you please explain how this script functions?
Don’t worry about it I also needed it for someone else. Each Block in the Tracks model defines an area which is one block section of tracks. The calls to AddLink specify which blocks are connected to eachother, this is the manual part.
CurrentBlock must return which block (a Model in the workspace) the given position maps to. It just raycasts down until it hits a part. This part is replacable depending on how you want your marker pieces to be shaped.
SetHighlightBlock just colors a model red, yellow, or gray, depending on ‘level’ being 2, 1, or 0 respectively. You could change this to toggle your lights and signs, since you have access to both the danger level and which train is in the block.
Calling UpdateBlock will check which blocks should be at danger or caution. It also calls SetHighlightBlock to visualize this.
There are some table shenanigans used to search the network for connected blocks, you don’t actually need to worry about how this works if you only need the results. There is no pathfinding involed, that’s a separate task, but it does use the same information about how blocks are connected.
Your pathfinder could use the total length left in the train’s path, and the trains speed, to update Next Train signs.
sorry to continue this i actually noticed an issue. I organized my things exactly how your setup was. possible issue: train is a model and so it is not noticed; OR, signal blocks are a folder so they aren’t noticed. any clue? edit: the train is also fairly long, and the position setting may be effected during curves or may cause an issue allowing some of the train to be in a caution block, meaning a collission could occur
You can make a specific part on the train be the “sensor” for blocks. Then you need to change the CurrentBlock function to get that instead of just the positions of every part in the Trains folder.