Why does this pause function not work whenever a part is touched?

So I found a Devforum post and a guy named Katrist made a train nodes and tweening script here and I wanted to just mess around with pausing tweens following nodes How i can make train in roblox? - Help and Feedback / Scripting Support - DevForum | Roblox and I am wondering why my code doesn’t detect whenever a part is touched and therefor does not pause. Any ideas?

--//Services
local TweenService = game:GetService("TweenService")

--//Variables
local Nodes = workspace.Nodes
local Train = workspace.TrainFolder.Train
local movementTween

--//Controls
local Speed = 40

--//Functions
local function Pause(tween)
	tween:Pause()
	print("paused")
	wait(5)
	tween:Play()
end

while Train:IsDescendantOf(workspace) do
	for i, node in ipairs(workspace.Nodes:GetChildren()) do
		local distance = (node.Position - Train.PrimaryPart.Position).Magnitude

		movementTween = TweenService:Create(Train.PrimaryPart, TweenInfo.new(distance/Speed, Enum.EasingStyle.Linear), {Position = node.Position})
		movementTween:Play()
		workspace.Part1.Touched:Connect(function(part)
			if part.Name == "Main"  then
				Pause(movementTween)
			end
		end)
		movementTween.Completed:Wait()
	end
end```
1 Like

If you want the train to stop at a specific node, change your script to this:

--//Services
local TweenService = game:GetService("TweenService")

--//Variables
local Nodes = workspace.Nodes
local Train = workspace.TrainFolder.Train

--//Controls
local Speed = 40

--//Functions
while Train:IsDescendantOf(workspace) do
	for i, node in ipairs(Nodes:GetChildren()) do
		local distance = (node.Position - Train.PrimaryPart.Position).Magnitude

		local movementTween = TweenService:Create(Train.PrimaryPart, TweenInfo.new(distance/Speed, Enum.EasingStyle.Linear))
		movementTween:Play()
		movementTween.Completed:Wait()
		
		if i == 5 --[[Reference the node number you want to stop at]] then
			task.wait(5)
		end
	end
end
1 Like

Thank you so much, it actually works! Have a wonderful day!

1 Like

No problem. If you have any more questions, feel free to ask.

1 Like

Alright, the last question I have is how I can stop the train slowly like physics(vehicleseat.Throttle) can, basically adding torque to it. Here’s an example of what I mean.

Only way to simulate this using the node system I did, is to just add a lot of nodes in between and tweak the speed variable so when the train passes over the nodes, it slows down smoothly.

For example:

--//Services
local TweenService = game:GetService("TweenService")

--//Variables
local Nodes = workspace.Nodes
local Train = workspace.TrainFolder.Train

--//Controls
local Speed = 40

--//Functions
while Train:IsDescendantOf(workspace) do
	for i, node in ipairs(Nodes:GetChildren()) do
		local distance = (node.Position - Train.PrimaryPart.Position).Magnitude

		local movementTween = TweenService:Create(Train.PrimaryPart, TweenInfo.new(distance/Speed, Enum.EasingStyle.Linear))
		movementTween:Play()
		movementTween.Completed:Wait()

		if i == 5 --[[Reference the node number you want to start slowing down at]] then
			task.spawn(function()
				for i = 1, 40 do
					Speed -= 1

					task.wait(0.1)
				end
			end)
			
			continue
		end
		
		if i == 10 --[[Reference the node number you want to stop at]] then
			task.wait(5)
			Speed = 40
		end
	end
end

[** I ended up fixing it by deleting node 0 and replacing it with node 1, Thanks for the help!**]

Hello, earlier today this was working but I just updated studio and now it does not stop at the specified node (it stops at 5, not 7 like in the script), and it prints waiting when just joining in therefor confirming its always waiting. What should I do?

--//Services
local TweenService = game:GetService("TweenService")

--//Variables
local Nodes = workspace.Nodes
local Train = workspace.TrainFolder.Train

--//Controls
local Speed = 40

--//Functions
while Train:IsDescendantOf(workspace) do
	for i, node in ipairs(Nodes:GetChildren()) do
		local distance = (node.Position - Train.PrimaryPart.Position).Magnitude

		local movementTween = TweenService:Create(Train.PrimaryPart, TweenInfo.new(distance/Speed, Enum.EasingStyle.Linear), {Position = node.Position})
		movementTween:Play()

		if i == 4 --[[Reference the node number you want to start slowing down at]] then
			task.spawn(function()
				for i = 1, 40 do
					Speed -= 1
					task.wait(0.1)
				end
			end)

			continue
		end

		if i == 7 --[[Reference the node number you want to stop at]] then
			print("waiting")
			task.wait(5)
			Train:MoveTo(workspace.Nodes["0"].Position)
			print("waited")
			Speed = 40
		end
	end
end```
1 Like

Alright, I have been working on my script since yesterday and I’ve made a simple train carriage system, but there are 3 things I’ve been trying to fix that I just have no idea how to. First off, my train carriage is not always the same distance away from the train and either goes too much forward or backward. Second thing, the thing that I assume is causing the first problem is that the carriage is trying to tween to the train’s primarypart, not the train’s primary part - a vector3 value like (-5,0,0). Lastly, I am wondering how I can move the whole model and not just the primarypart. Any suggestions @Katrist? My code is:

--//Services
local TweenService = game:GetService("TweenService")

--//Variables
local Nodes = workspace.Nodes
local Train = workspace.TrainFolder.Train
local Carriage = workspace.TrainFolder.Carriage1
local LastCarriage = Carriage

--//Controls
local Speed = 40

--//Functions
while Train:IsDescendantOf(workspace) do
   for i, node in ipairs(Nodes:GetChildren()) do
   	local distance = (node.Position - Train.PrimaryPart.Position).Magnitude
   	local carriageDistance = (Carriage.PrimaryPart.Position - Train.PrimaryPart.Position - node.Position).Magnitude
   	
   	local carriageTween = TweenService:Create(Carriage.PrimaryPart, TweenInfo.new(carriageDistance/Speed, Enum.EasingStyle.Linear), {Position = node.Position})
   	local movementTween = TweenService:Create(Train.PrimaryPart, TweenInfo.new(distance/Speed, Enum.EasingStyle.Linear), {Position = node.Position})
   	
   	carriageTween:Play()
   	movementTween:Play()
   	
   	movementTween.Completed:Wait()

   	Carriage:SetPrimaryPartCFrame(LastCarriage:GetPrimaryPartCFrame() * CFrame.new(Carriage.PrimaryPart.Size.X, 0, 0))

   	LastCarriage = Carriage
   	Carriage.Parent = workspace.TrainFolder
   	
   	--Node stops & slowdowns.
   	if i == 5 --[[Reference the node number you want to start slowing down at]] then
   		task.spawn(function()
   			for i = 1, 40 do
   				Speed -= 0.9
   				task.wait(0.1)
   			end
   		end)

   		continue
   	end

   	if i == 8 then
   		task.wait(5)
   		Train:MoveTo(workspace.Nodes["1"].Position)
   		Carriage:MoveTo(Train.PrimaryPart.Position)
   		Speed = 40
   	end
   end
end```

Could you send a video of the problems?

Here’s how to fix the train not moving though:

--//Services
local TweenService = game:GetService("TweenService")

--//Variables
local Nodes = workspace.Nodes
local TrainFolder = workspace.TrainFolder
local Train = TrainFolder.Train
local Carriage = TrainFolder.Carriage1
local LastCarriage = Carriage

--//Controls
local Speed = 40

--//Functions
while Train:IsDescendantOf(workspace) do
	for i, node in ipairs(Nodes:GetChildren()) do
		local distance = (node.Position - Train.PrimaryPart.Position).Magnitude
		local carriageDistance = (Carriage.PrimaryPart.Position - Train.PrimaryPart.Position - node.Position).Magnitude

		local carriageTween = TweenService:Create(Carriage.PrimaryPart, TweenInfo.new(carriageDistance/Speed, Enum.EasingStyle.Linear), {CFrame = node.CFrame})
		carriageTween:Play()
		
		local movementTween = TweenService:Create(Train.PrimaryPart, TweenInfo.new(distance/Speed, Enum.EasingStyle.Linear), {CFrame = node.CFrame})
		movementTween:Play()
		movementTween.Completed:Wait()

		Carriage:SetPrimaryPartCFrame(LastCarriage:GetPrimaryPartCFrame() * CFrame.new(Carriage.PrimaryPart.Size.X, 0, 0))

		LastCarriage = Carriage
		Carriage.Parent = TrainFolder

		--Node stops & slowdowns.
		if i == 5 --[[Reference the node number you want to start slowing down at]] then
			task.spawn(function()
				for i = 1, 40 do
					Speed -= 0.9
					task.wait(0.1)
				end
			end)

			continue
		end

		if i == 8 then
			task.wait(5)
			Train:MoveTo(Nodes["1"].Position)
			Carriage:MoveTo(Train.PrimaryPart.Position)
			
			Speed = 40
		end
	end
end

Here is a video of what my problem is:

Why do you have to also tween the carriage though? Can’t you just weld them together?

That is a good idea, but it has the problem about that is the whole reason I switched to CFrame tweening.

Also, I just realized why it goes into the train, it does not task.wait() or slow down because it tweens to the node the train is going to instead of staying behind the node the train is at. How can I fix this? @Katrist

@Katrist What should I do to fix my train carriages? It’s been over a week, and I still don’t know how I can fix it. Thanks!

Why is your carriage rotating while it’s welded?


I don’t understand, my carriage is moving, not rotating. If I weld anything to the main part just teleport away. My code once again is:

--//Services
local TweenService = game:GetService("TweenService")

--//Variables
local Nodes = workspace.Nodes
local CarriageNodes = workspace.CarriageNodes
local Train = workspace.TrainFolder.Train
local Carriage = workspace.TrainFolder.Carriage1

--//Controls
local Speed = 40
--//Functions
while Train:IsDescendantOf(workspace) do
	for i, node in ipairs(Nodes:GetChildren()) do
		local distance = (node.Position - Train.PrimaryPart.Position).Magnitude
		local carriageDistance = (node.Position - Carriage.PrimaryPart.Position).Magnitude

		local carriageTween = TweenService:Create(Carriage.PrimaryPart, TweenInfo.new(carriageDistance/Speed, Enum.EasingStyle.Linear), {Position = node.Position - Vector3.new(5)})
		local movementTween = TweenService:Create(Train.PrimaryPart, TweenInfo.new(distance/Speed, Enum.EasingStyle.Linear), {Position = node.Position})
		
		movementTween:Play()
		carriageTween:Play()
		
		movementTween.Completed:Wait()
		
		--Node stops & slowdowns.
		if i == 5 then
			task.spawn(function()
				for i = 1, 40 do
					Speed -= 1
					task.wait(0.1)
				end
			end)

			continue
		end

		if i == 8 then
			task.wait(5)
			Train:MoveTo(workspace.Nodes["1"].Position)
			Carriage:MoveTo(Train.PrimaryPart.Position)
			Speed = 40
		end

	end
end```

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.