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)
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.
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?
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 )
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?
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?
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?