How does this make sense?

Hey guys. I like math, but I personally suck at it, so I don’t know how this code makes sense. I’ve tried everything I can to understand it, but I just don’t get it. Thanks, if you know how to explain it to me! I know math is hard to explain because, well it’s math, lol.

local Duration = 3 -- It should take the listed time, which is 3 seconds in this case, to reach the goal below.
local Goal = 120 -- The number of studs the part will have moved in the listed time above.
local Minute = 60 -- Because 60 seconds are required to make a minute.
local Number = (Goal / Minute) -- The number is 2.
local Estimate = (Number / Duration) -- The stud increment, which is 0.6666666666666666.

Of course, after tweaking this to work in a RunService.Heartbeat script signal, the part stopped moving in 3 seconds with a slightly noticeable delay because of the heartbeats of course.
I tried dividing the goal with the duration, which meant that the part would reach 20 studs in a second.

It worked at first, but if the goal was increased, the speed would remain the same, which meant that the part didn’t stop moving at exactly 3 seconds. I’m glad it works, but why?

1 Like

Alrights those are some numbers. But what do you do with them?

So what it looks like you are doing is trying to get a part to move Goal studs in Duration seconds. First you will need to know how many studs per second it will go which is just Goal/Duration. Well, for that you should use the DeltaTime parameter passed from RunService.Heartbeat, meaning RunService.Heartbeat:Connect(function(deltaTime) end)

That delta time is the time in-between last frame and this frame. You would use this to know how far the stud should move this frame. This is where I like units. deltaTime is seconds, and so is Duration. Goal is in studs. You know that you want it to move Goal in Duration seconds, so to get the distance for this frame you will get the previously calculated studsPerSecond which has units of studs/second, and multiply it by the deltaTime to get a unit of studs. This is the studs that the part should move in that frame.

2 Likes

Or, you could just Tween the Part to the new location, with a Tween length of Duration.
You don’t even have to consider the other Parameters.
The Tween would take Duration seconds to move no matter what the distance is between the start Position and the end Position.

Thanks for that explanation! Not only is this more accurate, but you also were great at teaching me how this works and why it works. You should seriously consider being a math teacher, lol. Math is something that can be quite difficult to explain, so thank you for taking the time to explain it to me in depth. Have a good day/evening/night!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.