Accurate timing when moving parts through script for beginners

TL;DR: Accurate timing when moving parts through script for beginners - #3 by Wizzthepixelcat

Introduction.

Hey there! Sometimes, developers want accurate timing when it comes to part movement. For example, you might want a part to move 10 studs in 3 seconds. Maybe you want a part to rotate 45 radians in 10 seconds. But how would you do that? Most developers know it’s through math, and a lot of people hate math, and I can see why. (Like seriously, nobody is using algebra to count. Who was the maniac that decided to add letters in math?) Although we will not be needing any algebra for this, we will be needing to know basic addition, subtraction, multiplication, and division. And I will be your teacher, free of charge too :slight_smile:! Without further ado, let’s hop right in!

The real deal.

This is a rather short tutorial since it’s for beginners, so no need to panic! There are no dos and don’ts here. The only thing that we need as of now is an example. Let’s create one. We have a part in our workspace, and we want it to move 125 studs on the Z axis in 2 seconds. Well first, let’s add our goal.

local Goal = 125

Nice. Now that we have our goal, let’s add the duration, or in other words, the number of seconds that we want the part to move for.

local Goal = 125
local Duration = 2

Perfect. We have our goal and our time that should pass before the part stops moving. Now here’s where the math begins. We need an estimate of how many studs will be moved in one second. To do this, we need to divide our goal with our time limit. You can either get a calculator, add it up in your head, or simply follow along.

Answer: 125 / 2 = 62.5 (Note: The / symbol is the divide symbol.)

Now that we have our answer, let’s add it into the code!

local Goal = 125
local Duration = 2
local StudsPerSecond = (Goal / Duration)

Great. Now that we’ve finished that, we now need to multiply our StudsPerSecond by deltatime. But, how will we get deltatime? None other than RunService of course! How do we get RunService though? Well, quite literally, we need to get the service. Let’s do that!

local Goal = 125
local Duration = 2
local studsPerSecond = (Goal / Duration)
local RunService = game:GetService("RunService")

Now that we have the service, let’s make a RBXScriptConnection, which is something that most developers have probably already done at some point in their development years. An RBXScriptConnection is when you connect an event to a function. The function can either have a name or it can be anonymous. Anonymous functions are a lot easier for me but use whatever method you’d like!

local Goal = 125
local Duration = 2
local studsPerSecond = (Goal / Duration)
local RunService = game:GetService("RunService")

local Connection = nil -- Our variable that we will assign to the RBXScriptSignal below, which is RunService.Heartbeat. Without it, calling Connection:Disconnect() later would return nil, which means our script will error.

Connection = RunService.Heartbeat:Connect(function(deltaTime) -- Anonymous function. If I put RunService.Heartbeat:Connect(FunctionNameHere), then it would be a named function.
 
end)

Perfect! Our script has the necessary “pieces of the puzzle”! Let’s now add the finishing touches!

Finishing product.

local Goal = 125
local Duration = 2
local studsPerSecond = (Goal / Duration)
local studsTraveled = 0 -- We need this for our "math.min()" below.
local RunService = game:GetService("RunService")

local Part = script.Parent -- This can be changed to wherever your part's location is, like "workspace.PartContainer.YourPartNameHere" for example.
local Connection = nil -- Our variable that we will assign to the RBXScriptSignal below, which is RunService.Heartbeat. Without it, calling Connection:Disconnect() later would return nil, which means our script will error.

Connection = RunService.Heartbeat:Connect(function(deltaTime) -- Anonymous function. If I put RunService.Heartbeat:Connect(FunctionNameHere), then it would be a named function.
	local Increment = studsPerSecond * deltaTime -- Since deltaTime is a very small number, the value of our variable "studsPerSecond", which is 62.5, will not be increased to a greater number, but instead decreased to a much smaller number, so don't get confused!
	Part.Position += Vector3.new(0, 0, Increment)
	studsTraveled = math.min(Goal, studsTraveled + Increment) -- Counts the number of studs that the part travels, though making sure it won't count over our goal, which is 125.
	print("Studs traveled:", studsTraveled)
	if studsTraveled >= Goal then
		print("Complete!")
		Connection:Disconnect() -- Disconnects our RBXScriptConnection to prevent memory leaks.
		return
	end
end)

And voilà! We’ve added our finishing touches and now have the finished product! Thank you so much for reading this if you got this far! A more advanced tutorial may come in the future, and it will be linked here if so. I hope this helped and enjoy your accurate movement script!

Thank you for taking the time to view this tutorial! If you have any requests or suggestions, please let me know.

If you found any inaccuracies in my code or one of my statements, feel free to let me know. :slightly_smiling_face:

(Check out my other topic that teaches how to move parts with almost no lag at all!)

Topic link

How to move parts through script with almost zero lag!

Rate my topic? :pleading_face:

Rate the tutorial!
  • It was helpful!
  • It wasn’t very helpful…
  • I don’t understand it.
  • I’d rather not say.

0 voters

4 Likes