How to make a for i function and set that as a number to Lerp with?

Since this post wont allow me to do a long title here is the full title I made before:

How would I make a for function :GetChildren() and make it get the parts and set it as a number value and use it for a var be able to move it at any point with minimal scripting?

I’m trying to make a Part move to another part using Lerping (which I know how to do from a different post I made a while back) but I want to get all of the parts and be able to move them but I don’t want to make a var for each one, so I’m trying to solve a way with a for function using GetChildren() to get all of the parts and make them be able to move each one of them by just using a simple addon (Lua I don’t think does this but it’s just something so you can understand better): i[1] So basically i would be the parts and I would use the [] to get the part number so I could just use that for Lerping the part. Sorry if this is a bit complicated but I have searched many times and have never found a post like this, thanks!

1 Like

So you basically want to lerp multiple parts at the same time, and with the same alpha? (Alpha is the number in the lerp that goes from 0 to 1)

Then you would need two loops, as lerping itself needs a loop, and then you would need a loop to run through every part.

I would do something like this: (Red the comments!)

local group = -- The parent of the parts you need, the group or folder or anything

local lerpMax = 10 -- Set this to any number, the bigger it is, the smoother the lerp will be
-- However, picking a number too large will make the "animation" slow

-- First, you have to save the parts' starting position
local startingPositions = {}
for i, part in pairs(group:GetChildren()) do
	startingPositions[part] = part.Position
end

for i = 1, lerpMax, 1 do
	for a, part in pairs(group:GetChildren()) do
		part.Position = startingPositions[part]:Lerp(EndPosition, i/lerpMax)
		-- set the EndPosition to the position you want the parts to get to
	end

	task.wait() -- the bigger the wait, the slower the animation will be
	-- leaving it at nothing or 0 will make it run every frame.
end

(I haven’t tested the script)
I hope this helps!

Well I need the EndPosition to be the next part, for example Point1 would be the starting point then the EndPosition would be point2 and so on so on, I need a way to automate that so instead of having 500 lines of code I can have maybe 50.

Oh okay, that makes more sense now. Then do this:

local positionParts = -- the parent of "point1" and "point2" etc.

for i=1, #positionParts:GetChildren(), 1 do
	local pos = positionParts:FindFirstChild("Point"..tostring(i))
	if not(pos) then break end

	part.CFrame = pos.CFrame
	task.wait()
end

Make sure every part is named correctly, so it’s “Point1” or “Point2” (“Point” followed by their position number).

Thanks this works, but there is another problem. It seems like when it’s moving it’s trying to go to Point 3 but it goes to Point 2, so I need a way for it to go to the Point then go to the next one when done. Also I forgot to say that when it’s trying to go to Point 3 it gets stuck at Point 2 and is trying to get to Point 3. Here is my current code:

local points = script.Parent.Parent.Points
local speed = script.Parent.SpeedValue
local carrier = script.Parent.Parent.Carriers.Carrier1

while true do

local positionParts = points

  for i=1, #positionParts:GetChildren(), 1 do
  local pos = positionParts:FindFirstChild("Point"..tostring(i))
  if not(pos) then break end
  
  local dt = task.wait()
  local distance = (pos.Position - carrier.Position).Magnitude
  local speed = speed.Value*dt
  local estimatedTime = speed /distance
  local adjustedLerpAlpha = math.min(estimatedTime,1)

  carrier.CFrame = carrier.CFrame:Lerp(pos.CFrame,adjustedLerpAlpha)

  print(dt + distance + speed + estimatedTime + adjustedLerpAlpha)
  
  task.wait()

end
end

Also sorry for late response.

Try printing out some values, like estimatedTime and speed to check if they have to correct value. When going from Point3 to Point2, the reason why it could be stuck at Point2 because the alpha is 0, so it doesn’t move anywhere.

Also, why do you use Lerp to set the CFrames? It’s just going to make the movement sloppy and look like it lags behind. I don’t understand why it is needed here.

Well I’m trying to make cable sag for my game and I have tried many ways and using Lerping is by far the best way, tweens wont work because I want to change the speed of the lift with a value with 1 being slow while higher being faster. I have not found another way and it seems like a good way and I am so close that I just need this one last thing I am talking about earlier.
Screenshot 2023-07-30 111424

Basically the yellow points are the points, while the green is the carrier. I need the carrier to go to the end point but then goes to the next point so on so on. Currently the carrier gets stuck not even half way through, and with multiple points it just goes above the point and tries to go to the next one, I probably should’ve been more specific about me creating cable sag for my game for a Ski Lift.

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