How do I move a player with a part?

I’m trying to figure out how to make a player move with a part whenever they are stepping on it. Here’s the code I am using to make the part move.

function StageModule.ActivateRaisingParts(checkpointFolder, raisingpartFolder)
	
	for j, raisingpart in pairs(raisingpartFolder:GetChildren()) do

		local goal = {}
		goal.CFrame = raisingpart.CFrame + Vector3.new(0,15,0)
		local goal2 = {}
		goal2.CFrame = raisingpart.CFrame + Vector3.new(0,-15,0)
		local goal3 = {}
		goal3.CFrame = raisingpart.CFrame + Vector3.new(0,0,-20)
		local goal4 = {}
		goal4.CFrame = raisingpart.CFrame + Vector3.new(0,0,20)
		local goal5 = {}
		goal5.CFrame = raisingpart.CFrame + Vector3.new(0,0,-25)
		local goal6 = {}
		goal6.CFrame = raisingpart.CFrame + Vector3.new(0,0,25)
		local goal7 = {}
		goal7.CFrame = raisingpart.CFrame + Vector3.new(0,0,-60)

		local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true, 0)
		local tweenInfo2 = TweenInfo.new(6, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true, 0)
		local tween = TweenService:Create(raisingpart, tweenInfo, goal)
		local tween2 = TweenService:Create(raisingpart, tweenInfo, goal2)
		local tween3 = TweenService:Create(raisingpart, tweenInfo, goal3)
		local tween4 = TweenService:Create(raisingpart, tweenInfo, goal4)
		local tween5 = TweenService:Create(raisingpart, tweenInfo, goal5)
		local tween6 = TweenService:Create(raisingpart, tweenInfo, goal6)
		local tween7 = TweenService:Create(raisingpart, tweenInfo2, goal7)

		if raisingpart.Name == "1" then
			tween:Play()
		elseif raisingpart.Name == "2" then
			tween2:Play()	
		elseif raisingpart.Name == "3" then
			tween3:Play()
		elseif raisingpart.Name == "4" then
			tween4:Play()
		elseif raisingpart.Name == "5" then
			tween5:Play()
		elseif raisingpart.Name == "6" then
			tween6:Play()
		elseif raisingpart.Name == "7" then
			tween7:Play()
		end
	end		
end

Can anyone help me out with this one?

It’s better to use bodymovers aka bodyvelocity on the brick to move 1 needed direction as it is able to transport the player with it and it can be set to 0 with 1 line of code

1 Like

Ok. I’ll try that. Thanks for the help.

Hey, take a look here:

1 Like

This solution may allow me to maintain the progress I’ve already made. Thanks for the help!

1 Like

You have to use the pathfinding AI, and if you want for physics while the part moving, use bodygyro.

Hi!

The following example is one of the ways to use BodyMovers as a solution to your problem, more specifically BodyVelocity, exactly like @Ie_fishe proposed. I’m almost certain some snippets of appended module script can be even more efficient, however, the example should be good enough, works well, and can be improved any time.

It’s easier to manage configurations when we store everything in tables. Here, I store data about travel velocities of the moving parts inside a dictionary-type table, along with timing.

Keep in mind again, there are different ways to make a moving platform capable of carrying a player with it. A very good example is moving plate in open sourced Platformer character controller by @EgoMoose .

As long as BodyMovers are involved, Roblox engine takes care of physics. In this case, I have everything we need stored in a folder named MovingParts inside Workspace:

image

Components:

  • PartMover - a module script (which should be put in ServerStorage later, as that is the best practice, and also to ensure exploiters can’t read it’s content);

  • Parts - each part has to be named differently (I prefer differing them using numbers) and their indexes in module script table have to share the same name as well. To apply forces, a prerequisite is unanchoring them;

  • BodyGyro - prevents parts from changing orientation (spinning). Make sure MaxTorque and P are very high numbers, so stability is ensured;

  • BodyVelocity - it’s properties are changed from script;

  • A normal server script - manages part touch detection and is present in every part.

What about scripts? Isn’t it somewhat impractical to have this one script in every single moving part, especially if the place requires large amounts of parts? Yes, that is why we have a nice option to use tagging. Read more about Collection service here.

Before using anything, you will have to modify table yourself.

Good luck with your projects!

(Finally, the link to uncopylocked place:)

https://www.roblox.com/games/6363605319/Moving-platform

Module script code
local PartMover = {}

PartMover.MovementInfo = {
	["Part1"] = {Vector3.new(25,0,0), Vector3.new(-25,0,0), 3.5}; -- velocity, back velocity, time (s)
	["Part2"] = {Vector3.new(0,10,0), Vector3.new(0,-10,0), 3};
}

function PartMover.ActivatePart(part)
	if (PartMover.MovementInfo[part.Name] and not PartMover.MovementInfo[part.Name][4])
	then
		local moveTime = PartMover.MovementInfo[part.Name][3]
		
		local bodyVel = part.BodyVelocity
		bodyVel.MaxForce = Vector3.new(500000, 500000, 500000)
		PartMover.MovementInfo[part.Name][4] = true
		
		bodyVel.Velocity = PartMover.MovementInfo[part.Name][1]
		wait(moveTime)
		
		bodyVel.Velocity = PartMover.MovementInfo[part.Name][2]
		wait(moveTime)
		
		bodyVel.Velocity = Vector3.new(0,0,0)
		wait(1)
		PartMover.MovementInfo[part.Name][4] = false
	elseif (not next(PartMover.MovementInfo[part.Name])) then
		warn("No data about " .. part.Name .. " found.")
	end
end

return PartMover
Script code
local PartMover = require(script.Parent.Parent.Parent.ModuleScript)

script.Parent.Touched:Connect(function(hit)
	if (hit.Parent:FindFirstChild("HumanoidRootPart")) then
		PartMover.ActivatePart(script.Parent)
	end
end)
2 Likes

This is an amazing tutorial, and I cannot thank you enough for this! I haven’t been able to find a tutorial like this that explains how this system works. Thank you! This will be amazing for creating another portion of my game.

1 Like

This worked perfectly! Thank you so much for the tutorial! You really helped me out here!

1 Like

No problem! I’m glad that I could and happy that it works.

All the best!

1 Like