I would like to achieve a system where if a certain part passes into a area, a part changes colour. And when it goes into a new area, the previous block changes colour.
local part = script.Parent
local function == destroy(otherPart)
if otherPart.Name == "319CAR1" then
if true then
end
end
part.BrickColor.new = "Bright Red"
end
local part = script.Parent
local function == destroy(otherPart)
if otherPart.Name == "319CAR1" then
part.BrickColor.new = "Bright Red"
end
end
part.Touched:Connect(destroy)
You also had an extra/unnecessary if statement and the changing of the part color in the wrong area
So you want the green Parts to change colour when the train passes each one?
If you want the part to change colour back when the “319CAR1” passes by then you’d need a TouchEnded event.
Also, you probably shouldn’t name a function destroy since Destroy is used to get rid of instances and might cause confusion.
local part = script.Parent
local function onTouch(otherPart)
if otherPart.Name == "319CAR1" then
part.BrickColor.new = "Bright Red"
end
end
local function onTouchEnded(otherPart)
if otherPart.Name == "319CAR1" then
part.BrickColor.new = "Bright Green"
end
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)
I had a similar issue when trying to make a block signaling system for trains, and have had success using ray casting as a method of detecting where trains are. In past experience using touch detectors, they’re effective but I have noticed quite a few instances where a hit is not detected (this is problematic when trains are sometimes one car long)
if a certain part passes into a area, a part changes colour. And when it goes into a new area, the previous block changes colour.
I suggest giving rays a shot as when combined with a simple timer system, they can achieve this quite easily. A somewhat simple way to set it up is to have a script run through the marked segments of track every few seconds (a segment being a chain two or more marker blocks that it casts rays between), cast a ray at each one, and if it hits, light up the block. If it doesn’t hit for x seconds, turn the block off.
Not to deter you from using a touch system (I have seen many instances where they work fine), but just a suggestion if you’re having trouble.
In the picture the green block isn’t touching the train Part. I can’t tell if you were trying to move it to show me.
It also may be the fact that your train has a ton of Parts touching the block in a very short time. Try making the detector sit 5-10 studs higher than the train and weld the 319CAR1 block at the same height so that it only contacts the one Part.
If the train is travelling fast then the size of the small green block will only change while the 319CAR1 block is touching it.
You can also use a BlockMesh in the green block and change the Offset of the BlockMesh so it can sit above the train, but appear to be in front of the train, or wherever you want it. This would also allow you to make the detector block 100 studs long but appear to be 4x4x4 studs using the Scale settings of the BlockMesh. This would detect the hit for 100 studs, change the colour, and then change back after the train passes the 100 stud block.
Do you have CanQuery or CanTouch unchecked in the Properties of either the green block or the 310CAR1 Part?
If you do then they won’t register collisions.