I have this script and I want it to make an object move when someone goes on it, but I want it to move smoothly

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

local LastTrainCFrame

local Function
local Function2


Function = RunService.Heartbeat:Connect(function()

--------------------------------------------------------------- CHECK PLATFORM BELOW

local RootPart = player.Character.LowerTorso

local Ignore = player.Character

local ray = Ray.new(RootPart.CFrame.p,Vector3.new(0,-50,0))

local Hit, Position, Normal, Material = workspace:FindPartOnRay(ray,Ignore)

if Hit and Hit.Name == "RaftTop" then -- Change "RaftTop" to whatever the moving part's name is

--------------------------------------------------------------- MOVE PLAYER TO NEW POSITON FROM OLD POSITION

local Train = Hit
if LastTrainCFrame == nil then -- If no LastTrainCFrame exists, make one!
	LastTrainCFrame = Train.CFrame -- This is updated later.
end
local TrainCF = Train.CFrame 

local Rel = TrainCF * LastTrainCFrame:inverse()

LastTrainCFrame = Train.CFrame -- Updated here.

RootPart.CFrame = Rel * RootPart.CFrame -- Set the player's CFrame
--print("set")

else
LastTrainCFrame = nil -- Clear the value when the player gets off.

end

Function2 = player.Character.Humanoid.Died:Connect(function()
	Function:Disconnect() -- Stop memory leaks
	Function2:Disconnect() -- Stop memory leaks
end)

end)

This is a script that is supposed to make an object move slowly, but I dont understand the parts where it says to make a LastTrainCFrame and also a player CFrame. I know it has something to do with the camera, but I never used it before. can someone help me with this?

2 Likes

Use the CFrame:Lerp() function. Search it up, really useful for making smooth movement

is there any error in line 15?

oh actually it doesnt show the lines here, but it says “attempt to index nil with character”

where does it show the player cframe please help me

Train.CFrame is the CFrame you need.

Or HumanoidRootPart.CFrame.Try it with both :smile:

That code is from the jailbreak train thing if I’m correct, if so, that code is only meant to make the player move ontop of cframed parts/anchored parts. Not:

You’ll have to script the object moving part yourself, the script above is only meant to make the player move above the cframed parts as usually cframed parts dont have the player moving along with it.

You can make a tween too. It can probably work

but I just dont know how to finish the script. sometimes i just want to give up on leaning how to script because its so confusing sometimes. the parts where there are notes says that i have to update the script to make it work in my game because everyone’s game is different. i just dont know what cframe is and i dont know where it shows the cframe. and i also dont know what LastTrainCFrame is. its telling me to make one but i only know a little scripting. then the next part tells me to change it to my player’s cframe, but I dont even know where to find my player’s cframe. in somerandom places it says nil and i know that nil means that there is no information there. Im good at so many things, but this programming is just so hard for me. I just want to be like everyone else and know how to do this. i watch so many videos, but i forget what was being said in the videos because there is so much information. i dont know what to do.

what is a tween and what are offset properties. im trying to find them in the studio in the properties tab and they are not even there.

There are some tutorials of how to tween in YouTube and in the Developer wiki

CFrame is a property of a part’s position/rotation in the world. It can be accessed by part.CFrame. Programming is not a thing you learn overnight, try working your way up from the basics to more advanced things. Right now you are being a little ambitious right now.

1 Like

You could use a loop and make your part move by 1 to 5 studs. After each iteration, wait 0.01 seconds.

This will make your part move smoothly.

I don’t know if this will solve your problem.

Never do this, this is actually a very inefficient way of moving things, Instead use tweenservice or some kind of force.

1 Like

I forgot to say that the method I posted above should only be used as a last resort.

Basically only use that method if you can’t do anything else.