Train System made with Align Position/Orientation acts like a treadmill when walking on it

Hello,
I recently made a train system using Align Position/Orientation. What is supposed to happen is when a player walks on the train they move with the train. The script is a local script placed in StarterPlayerScripts. My old train ran on tweening the CFrame of the primary part of the train. The script worked perfectly there, but now since I switched to align position, the player gets shot down the train like it is a conveyor! I want the players to be able to walk on the train like how it used to work when it was ran with CFrame.

I hope you can help, thanks.

Player on train script

--//Variables\\--

wait(1+math.random())

local TrainFolder = game.Workspace.Train
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer

local LastCF
local LastPart


RSFunction = RunService.Heartbeat:Connect(function()
	
	local Root = Player.Character.HumanoidRootPart
	
	local ray = Ray.new(Root.CFrame.p, Vector3.new(0,-20,0))
	
	local hit = game.Workspace:FindPartOnRayWithIgnoreList(ray, Player.Character:GetChildren())
	
	if (hit and hit:IsDescendantOf(TrainFolder)) then
		
		if hit ~= LastPart then
			
			LastCF = nil
		end
		local Train = hit
		
		if not LastCF then
			
			LastCF = Train.CFrame
		end
		
		local TrainCF = Train.CFrame 
		
		local Rel = TrainCF * LastCF:Inverse()
		
		
		LastCF = Train.CFrame
		Root.CFrame = Rel * Root.CFrame
		
		LastPart = Train
		
		else
			
			LastCF = nil
		end

end)

for i,v in pairs(game.Workspace.TrainStop:GetChildren()) do
	v.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and LastCF ~= nil then
			hit.Parent.HumanoidRootPart.CFrame = CFrame.new(-104.85, 2.9, 152.4)
		end
	end)
end

DiedFunction = Player.Character.Humanoid.Died:Connect(function()

	RSFunction:Disconnect()
	DiedFunction:Disconnect()
end)

Video:

https://gyazo.com/b30269f12fc4724dbe94fe00070f2d8f

2 Likes

If I scripting was my expertise, I’d be able to help.
Sorry! Also really good work, hopefully somebody will be able to help you.

1 Like

this should help you:

edit oof

1 Like

That is not my problem, my problem is the train acts as a conveyor rather than what it is supposed to do. Thanks for your help though.

Check if the actual position property of the part is changing, sometimes body movers don’t change the position property.

If it’s not maybe use the position of the attachment it’s connected to?

1 Like

Your code works I believe, however AlignPosition and AlignOrientation already have a built system to keep the player on the moving platform anyways, so your code is completely unnecessary. I tried this before on a ship, and sadly it had the same result. Somehow the humanoid’s default behavior of sticking on the platform and the raycasting code combines to make the character slide.

1 Like

Hm, I disabled the script and it still seems like it has the same effect.

I might be wrong then. Let me get a baseplate and test your code.

1 Like

Would you like me to send a file of the game?

https://gyazo.com/6c166820763d798b86bc1483db34470e

I’ve tested with an AlignOrientation and AlignPosition and it keeps the humanoid attached (except in cases where I move it too harshly)

1 Like

I think the issue is the player is stepping on things that are welded to the part with align position and orientation, causing the system not to make the player move along

It works with welds, I know for a fact. One thing I noticed is that the replication behavior of this is extremely weird. Are you setting the attachment positions on the Client or the Server, and who is the network owner?

1 Like

Network Owner only works on parts that arn’t welded or anchored, and I am setting the attachment positions on the server. How my system works is that the attachments of the align position are never changed, the attachment1 of the align position is a part that gets tweened with CFrame

Do you think you could add me on discord so I could send you a file of the game?

If you’re using align orientation and align position, then the parts are unanchored. It works on welded parts that are welded to an unanchored object. You must be changing the attachments, since thats the only way the train can move? I am confused.

1 Like

The attachments arn’t the ones changing, the part that it is attached to is changing.

You said you tweened the attachment? AlignOrientation and AlignPosition work by changing the attachments’ positions to make it align itself

Nevermind, i see what you mean

If you’re tweening a part, why not just tween the train itself instead of using AlignOrientation/AlignPosition as a middleman so to speak.

1 Like

That was my old system, it was overall very buggy and choppy. I wanted to be able to control the CFrame as well as benefits of the physics simulation all at once

Seeing your gif, it seems like you want a wild west inspired system. I believe that game has the train anchored completely and instead uses a custom method of calculating the train’s CFrame. Since it follows a set track, I cant imagine this being too difficult. You can do an equation like speed = speed + acceleration * dt and position = position + speed * dt.

1 Like