Cart that goes on a train track

Hello!

Recently me and my friend made a train-cart-type vehicle that goes on a rail. And I have attempted to try script it but it went completely wrong.

I would like to know how to get this thing to move, but I am not sure how. It needs to follow a set of train tracks with bends and stuff.

Any help if greatly appreciated, thanks :slight_smile:

4 Likes

Do you want it to react to physics or just be a smooth ride (no physics involved)?

(reacting to physics means like able to fall off the track, able to be pushed off, able to be moved, etc.)

2 Likes

Ideally a smooth ride, to prevent users getting angry.

1 Like

You should use TweenService to tween the cart to each part of the track. If you could show me the explorer it would help make a script for you.

Cart:
image

Track:
image

Is union the wooden part of the track?

The union is the metal and the wooden part of the track, I can re-name the unions if that helps.

Wait so does 1 of those parts named union include BOTH the wooden and metal parts?

No, one union is the wooden part and the other union is the metal part.

Could you rename them respectively to what they are? If it’s too hard that’s fine but renaming would be preferable.

image

Sorry for not mentioning earlier but is it possible that you can rename each track section with its corresponding number? It would help with performance because instead of checking the distance every frame it could just check if the number is correct.

image
Like that?

Yes, that would be preferable.

Put this inside of StarterPlayerScripts and see if it works:

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

--//Variables
local Cart = workspace.Cart --//Whatever it's called
local TrackSections = workspace.TrackSections --//Whatever it's called

--//Functions
for i = 1, #TrackSections:GetChildren() do
	local trackSection = TrackSections:WaitForChild(i)
	
	local cartTween = TweenService:Create(Cart.PrimaryPart, TweenInfo.new(1, Enum.EasingStyle.Linear), {
		CFrame = trackSection.Metal.CFrame + Vector3.yAxis * 5
	})
	
	cartTween:Play()
	cartTween.Completed:Wait()
end

You will need to tweak some things but we can go over that after you can test it.

ALSO, only change the name of the tracks if you want to, if there are even just 50 tracks it will be a pain to name them all so you don’t have to, just make sure to tell me what you choose to do though.

Metal is not a valid member of Model “Workspace.TrackSections.1”

I am given that error, I checked into it. All the track models have Metal inside of it which makes me confused…

I would advise against tween service for this. TweenService is specifically designed to ignore physics limitations. While it’s smooth, this is pretty bad for cart-ride games. Relying on vehicle physics instead is much better.

How would I use vehicle physics instead then?

He said he wanted it to be smooth and to ignore physics. Read above.

Set up your cart like a vehicle. You want

  • free hinges connecting the axles to the body to let the axles rotate to match the track’s rotation
  • motorised hinges connecting the wheels to the axles to let the cart move forwards and backwards
  • [optional] spring constraints for damping and cool smooth cart rides that aren’t rigid. Take a look at “cart ride around nothing” for inspiration for this one.
1 Like