Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make the part slide to a location in EXACTLY a certain amount of time (smoothly)?

I tried the script from this link from the Wiki to make a part slide smoothly to another location.

Here is the link.

However, when I tried it on the actual part. It only moves extremely slowly, and it takes longer than the amount of time I expect to get to the target location. I don't know why. It will be great if you explain how this works as well (with your OPINION, not stuff from the wiki). I am looking forward to check out your thoughts! :)

Here is the code:

local Part = script.Parent.Parent:WaitForChild("KillerWall") -- this is the Part we will move
local newPos = Part.Parent:WaitForChild("WallStop").Position -- the position the Part will go to
local Time = 10 -- the time that the script will take to move the part
local Increment = 0.1 -- the Part will move this amount of studs each time it moves
local Debounce = false
 
local Diff = newPos - Part.Position -- the difference between the two positions
local Mag = Diff.magnitude -- the distance between the two parts
local Direction = CFrame.new(Part.Position, newPos).lookVector
 
script.Parent.Event:connect(function () -- function to move the Part
	if Debounce then return end -- end the function if debounce is true (just in case the event is fired several times.)
	Debounce = true -- make Debounce true so the function can't run
	for n = 0, Mag, Increment do
		Part.CFrame = Part.CFrame + (Direction * Increment)
		wait( (Time/Mag) * Increment ) -- This is the problem!!! (I think...)
	end
	Debounce = false -- set Debounce to false so the function can run again
end)

1 answer

✓ Accepted answer

In the wiki version, there is a Time variable same goes with the script you're presenting. I assume adjusting that value of the Time variable will change your speed.

Simple breakdown

  • The variable Diff/Mag accesses the numerial differences in position between newPos and Part (ex. So if two parts were two studs apart, then the magnitude between these parts would be two studs)

  • Variable Direction gets the direction from Part1(newPos) to Part2(Part)

  • Line 15 is where the part is moved by using CFrame, using just Direction would move it by one stud(default), so multiplying by Increment would change the length movement to Increment's value, now adding(+) that to Part's CFrame would cause movement by .1 studs

  • Line 16 is a tough one, dividing(let's say 10 seconds) by the magnitude of newPos and Part(ex. two studs) would give, 5 seconds if I am correct, that's pretty long, as the wiki says Since we aren't running this every second, we're running it every Increment seconds, we have to multiply that number by Increment..., so 5 multiplied by .1 = .5, so for every .5 seconds it will move by .1(Increment)

4 comments

0

one more thing, when I set the Time to 1, it takes about 40-45 seconds for the part to move to its location. What gives??? (srsly, im confused...)

0

Well, first. Are you mulitplying it by the increment? wait( (1/Mag) * Increment )

0

then i want the part to finish moving in EXACTLY the number from the Time variable.

0

thx i love how u explain the stuff to me.

This discussion is preserved as it was. New answers, comments, and votes are closed.