Train System - Stopping feedback

Hey guys!

I recently been developing my first train system, and I used an already made train and railtracks from a post here, and it works great, I’ve added a VectorForce in order to actually move it, and used RunService.

Now comes my issue, I want to add Stops as well, as the train is going to move by it’s self and rotate around the whole map and will stop for a said amount of time in each stop on it’s way which will be numbered (1,2,3,etc).

However, it simply isn’t working and just stops forever, I have a backpart and a frontpart (so the train doesn’t reverse because of the frontpart collision.

Code:


--// Variables

local Holder = script.Parent.Parent
local Base = Holder.Base

local stops = workspace.Stops

local RunService = game:GetService("RunService")

local Values = script.Parent.Values
local TrainDelay = Values.TrainDelay
local StopTimer = Values.StopTimer
local Force = Values.Force
local TrainMove = Values.TrainMove
local StopInProgress = Values.StopInProgress

local currentStop = 1
local totalStops = #stops:GetChildren()

--// Functions

function moveTrain()
	wait(TrainDelay)
	if TrainMove.Value == true then
		Base.VectorForce.Enabled = true
	else
		Base.VectorForce.Enabled = false

		wait(StopTimer.Value)
		
		
		stops[currentStop].CanCollide = false
		stops[currentStop].BackStop.CanCollide = false
		stops[currentStop].CanTouch = false
		if currentStop == totalStops then
			currentStop = 1
		else
			currentStop += 1
		end
		print(stops[currentStop].Parent)
		print(stops[currentStop])
		print(stops)
		print(totalStops)
		stops[currentStop].CanCollide = true
		stops[currentStop].CanTouch = true
		StopInProgress.Value = false
		TrainMove.Value = true
		print('all worked?')
	end
end

function StopTouched()
	stops[currentStop].BackStop.CanTouch = true
	stops[currentStop].BackStop.CanCollide = true
	TrainMove.Value = false
	moveTrain()
end

--// Initialization

Base.VectorForce.Force = Vector3.new(-Force.Value*1000,0,0)

RunService.Heartbeat:Connect(function()
	if StopInProgress.Value == true then return end
	moveTrain()
end)

stops[currentStop].Touched:Connect(function(hit)
	if StopInProgress.Value == true then return end
	StopInProgress.Value = true
	print("meh")
	StopTouched()
end)

Output:

  19:10:00.291  meh  -  Server - TrainMovementHandler:72
  19:10:03.307  Stops  -  Server - TrainMovementHandler:41
  19:10:03.308  2  -  Server - TrainMovementHandler:42
  19:10:03.309  Stops  -  Server - TrainMovementHandler:43
  19:10:03.309  2  -  Server - TrainMovementHandler:44
  19:10:03.310  all worked?  -  Server - TrainMovementHandler:49
  19:10:03.343  Stops  -  Server - TrainMovementHandler:41
  19:10:03.343  1  -  Server - TrainMovementHandler:42
  19:10:03.344  Stops  -  Server - TrainMovementHandler:43
  19:10:03.345  2  -  Server - TrainMovementHandler:44
  19:10:03.345  all worked?  -  Server - TrainMovementHandler:49
  19:10:03.425  meh  -  Server - TrainMovementHandler:72

Any help would be appreciated, thank you.

image