Welding a player to a train to prevent movement

I wanted to make a player stick to a part when the train is moving (For here I used the unweld.Disabled), and weld it with the train so that they will move together, and remove the weld when the train stops (the unweld script disable = false), here is my script so far.

I’ve currently successfully made the player weld to the train floor but not unwelding them when the train comes to a stop, here is my script so far.

local plrs = {}
local trainStatus = false
local TrainWeldModule = {}

script.Parent.Touched:Connect(function(p)
	repeat
		wait()
	until script.Parent.unweld.Disabled == true
	
	if p.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(p.Parent)
		local players = game.Workspace:FindFirstChild(p.Parent)
	local v = script.Parent
		p.Parent.HumanoidRootPart.Anchored = true
		p.Parent.Humanoid.PlatformStand = true

	local preWeld = p.Parent.HumanoidRootPart.CFrame
	local weld = Instance.new("Weld")
	weld.Part1 = p.Parent.HumanoidRootPart
	weld.Part0 = v
	weld.Name = "TrainWeld"
	local val = v.CFrame:Inverse() * preWeld
	weld.C0 = val
	weld.Parent = p.Parent.HumanoidRootPart
	p.Parent.HumanoidRootPart.Anchored = false
	
		repeat
			wait()
		until script.Parent.unweld.Disabled == false
		print("unwelding")
	local char = players.HumanoidRootPart
		if char.HumanoidRootPart:FindFirstChild("TrainWeld") then
			print("Confirmed")
		char.HumanoidRootPart.TrainWeld:Destroy()
		char.Humanoid.PlatformStand = false
			
			print("unwelded")
end
	end
end)
1 Like

Try using a WeldConstraint instead. It might be easier, I don’t know.

1 Like

Does this script work any better?

local plrs = {}
local trainStatus = false
local TrainWeldModule = {}

script.Parent.Touched:Connect(function(p)
	repeat
		wait()
	until script.Parent.unweld.Disabled == true

	if p.Parent:FindFirstChild("Humanoid") then
		local Char = p.Parent
		local v = script.Parent
		Char.HumanoidRootPart.Anchored = true
		Char.Humanoid.PlatformStand = true

		local preWeld = p.Parent.HumanoidRootPart.CFrame
		local Weld = Instance.new("WeldConstraint")
		Weld.Part0 = Char.HumanoidRootPart
		Weld.Part1 = v
		Weld.Name = "TrainWeld"
		Weld.Parent = Char.HumanoidRootPart
		p.Parent.HumanoidRootPart.Anchored = false
		repeat
			wait()
		until script.Parent.unweld.Disabled == false
		print("unwelding")
		local Root = Char.HumanoidRootPart
		if Root:FindFirstChild("TrainWeld") then
			print("Confirmed")
			Root.TrainWeld:Destroy()
			Char.Humanoid.PlatformStand = false
			print("unwelded")
		end
	end
end)

It unwelds but my character gets knockbacked a lot

Hello FrostedMattRBX

So a lot of people have tried to accomplish keeping players on a train. A solution that is used in the wide popular experiences such as Jailbreak and MadCity, when the train is moving, the train will not only move the itself, but you would also scan to see if there are players on board. The players on board will be moved with the train if you script it so their humanoidroots are moving at the same pace as the train.

Benefits of this means that you wouldn’t need to weld them and you’d probably be able to walk around the train whilst it’s moving.

If you want to research a lot more on this theory, the devforum has provided many posts where people have done this.

Hope this is helpful!

Ah, thats helpful, but what I’m trying to accomplish here is to weld them because people can lean to a specific side and derail the train, thats why, thanks tho! :smiley:

Maybe destroy the weld at the end since you’re instancing the weld every time the player gets on the train. I hope this has helped you.

The script that NeonTaco135 gave me earlier does that, but the player gets flinged after destroying