How do I go about welding a player to a tweened train

  1. What do you want to achieve?
    I am currently developing an automatic train game and I want to ensure that when the train is in motion, a player is welded to a part that is welded to the train and that as the player is welded, the user is moving with the train.

  2. What is the issue?
    Currently, the player is not welded to the part I want it to be welded to nor is the player moving along with the train. I tried adding a weld script to my script but the train still ends up leaving the player behind and the player is not welded to the intended part. For reference, here is a YouTube link containing a 3-second clip illustrating the issue (note that the translucent red brick is the part I intend to weld the player to): https://youtu.be/d35bmjzQ-eQ

  3. What solutions have you tried so far?
    I have tried importing some weld scripts on devforum and using the if-then command to find the player’s HumanoidRootPart and then anchoring that to the intended part but to no avail.

To summarize everything:
The train will wait 2 minutes at its current position for players to get on. The part that I intend to weld the player to on the train is called “SensorPart” (this part is already welded to the primary part that is tweening the train). After 2 minutes, the door closing announcement plays (which is defined as the local variable “announce” in the script) and 2 seconds after the completion of the announcement, that’s when I want the weld script to be activated before the train goes into motion. Once the train has reached its destination, the player should no longer be welded to “SensorPart.”

Note: I do not want the player to move freely whilst the train is in motion.

Now, I am an absolute novice at programming. I’m more of a developer than I am a scripter so your input and support are highly appreciated.

local TweenService = game:GetService("TweenService")
local train = workspace.Tweentrain
local modelroot = train.Door1
local announce = workspace.Sound1
local base = train.SensorPart

local infohangar = TweenInfo.new(8, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local infoturn = TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

local Fromdepot = TweenService:Create(modelroot, infohangar, {
	CFrame = CFrame.new(11, 7.5, -217)
})

wait(120)
announce:Play()
wait(0.1)
base.Touched:Connect(function(p)
	if p.Parent:FindFirstChild("Humanoid") then
		print('Player is on the floor')
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = p.Parent.HumanoidRootPart
		p.Parent.HumanoidRootPart.Anchored = true
		print('Player is now welded to the train')
	end
end)
Fromdepot:Play()
Fromdepot.Completed:Wait()

wait(0)
local TurnRight = TweenService:Create(modelroot, infoturn, {CFrame = CFrame.new(Vector3.new(11.208, 7.5, -221.773)) * CFrame.Angles(0,math.rad(-5),0)})
base.Touched:Connect(function(p)
	if p.Parent:FindFirstChild("Humanoid") then
		print('Player is on the floor')
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = p.Parent.HumanoidRootPart
		p.Parent.HumanoidRootPart.Anchored = false
		print('Player is no longer welded to the train')
	end
end)
TurnRight:Play()
TurnRight.Completed:Wait()

maybe you could set a primary part on the train, and weld the player to that primary part, also in your code it seems there is no weld.Part1., unless thats already pre-defined somewhere outside the script.

Umm…

	    local weld = Instance.new("WeldConstraint")
		weld.Part0 = p.Parent.HumanoidRootPart
		p.Parent.HumanoidRootPart.Anchored = true  -- you just Anchored the HRP.

And as @meowcat said, you aren’t setting weld.Part1
And do your prints show up in the output window?

Also wait.(time) has been deprecated. You should use task.wait(time).

I see. I’ll try setting a weld.Part1 and see how it goes.

Only (‘Player is on the floor’) prints, the other prints do not appear. But I’ll definitely be making modifications to the weld script and will provide an update later.

Did you try taking out the .Anchored line that I noted? If you Anchor the player’s HRP it isn’t going to move, even if it’s welded.

So I scoured Devforum for a while and I finally got the player welded to the train and yes I did take out the .Anchored line from the script and the weld function works marvelously. Thanks to @bbackstab as well, defining weld.Part1 was a must haha. Thanks to this post: Weld Player to floor and prevent movement. I used a module script instead and then in my main script (with some modifications in defining the local variables) I would just call the module from the module script to weld the player and in addition to the train moving, the player is welded to the train as it is in motion.

1 Like