Help with my train system

Hello! I was building a train system where when the train hits a part with a certain name, it will adjust its speed accordingly by disabling/ enabling scripts.

However, when I tried it by running a test, the train doesent disable the scripts I wanted and did nothing.

script in the train itself:


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



(i spent the couple hours trying to figure this out but to no avail)

Any help is appreciated!

What script are you trying to disable? script.Enabled = false would disable the current script, the one your train is running on.

Oh right, disabling a script doesn’t stop it from running.

Im trying to disable the current script that it is on and enable the script named “STOP1” but it does not work, the current script’s Enabled is still set to true and stop1 is set to false.

Could you add in a print statement?
I don’t think you’re reaching the setter code.

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?