Help with my train system

I added a print statement and it didnt print it out when it touched the part. Looks like its from another script. Your right, Im not reaching the setter code.

Also fyi the part (named stop1) does not have any scripts inserted into it.

is script.parent the exact part you want to get touched or is it a model?
is that exact part touching a part called “stop1”?
are both parts cancollide set to true?

script.parent is a part that the code is inserted to, and yes, the part that its touched is indeed “stop1”.

what method are you using to move the train around e.g tweenservice?

I use TweenService to move my train.

your script has a while loop in it that runs everthing right?

I use a for loop that runs until the train cant find any more parts/ tracks to tween to in a specific folder which houses all the train tracks.

basically in each iteration run this:

local PartA = PartOfTheTrain -- the bit of the train u want to collide 
local PartB = Stop1Part
local HowCloseYouWantThemToBeToActivate = 50--this is in studs
if (PartA.Position - PartB.Position).Magnitude < HowCloseYouWantThemToBeToActivate then
	--enable script 2
	--disable script 1
end

what that does is it will find the distance between the two parts each iteration and if its closer than 50 studs in this case it will run the if statement

It seems like it still does not work, no matter how much I set the distance to be. I’ve added a print statement but it does not print. The train the script is in is a part.

(edit: I have instead tried to just add a delay (wait()) before disabling and enabling the scripts and it does work, but I would need to be precise about the timing.

Well that would mean no hit is detected or a hit is detected but the name of the hit doesn’t equal “stop1” (obviously :wink:)
Are you sure the parts are actually touching? Maybe make them visible if they’re transparant, just to be sure.
Is the ‘CanTouch’ property enabled for both parts and are they in the same Collision group?

I have made both parts visible (Transparency is set to 0), I have also ensure that both parts have CanTouch set to Enabled.

script.Parent.Touched:Connect(function(Hit)
	print("Hit!")
end)

I instead tried this.

And it did not work. So basically nothing is detected despite the fact that when I ran the code, the train literally has touched the part. Should I rewrite all of my code?

Is the train a single mesh and not made up of multiple parts?

Its made by multiple parts welded to a main part, where the scripts are in.

Is the logic for this hit happening on multiple parts then?

No, just that single part where all the scripts are in.

I assume the main part is invisible and encompasses the entire model? If so you would need to disable CanCollide on all the other parts to ensure that any logic is handled purely by the main part. Or is the stop part big enough to guarantee that the main part hits it?

You added a new hit test. Use your prints on code you already have.

script.Parent.Touched:Connect(function(Hit)
	if Hit and Hit.Name == "stop1" then
		script.Parent.STOP1.Enabled = true
		script.Enabled = false
	end
end)

This script will hit stop many times with no debounce.
Start off by printing hit just to see what’s going on
if Hit and Hit.Name == “stop1” then – no need to test hit twice here also.

Could you provide a video of the train and the relevant parts touching?
Also show us where the script is located from the explorer.

It still does not work, the stop part is big enough for the train to touch it.

The problem is it does not print at all let alone actually work.