I'm trying to make a part move when the player presses the A key and I want to use linear interpolation to do it, which I'm not very familiar with. The code below moves the part to the desired position but then pauses for a few seconds before it can move again, and I'm not really sure why. How do I make the part move continuously?

function lerp(direction)
	local pos = a.Position + Vector3.new(50,0,0)
	for i = 1,100 do
		if holdingA == true then
			local alpha = i/100
			a.Position = a.Position:Lerp(pos, alpha)
			print(pos)
			wait()
		end
	end
end

uis.InputBegan:connect(function(inputObject)
	if inputObject.KeyCode == Enum.KeyCode.A then
		holdingA = true
		while holdingA do
			lerp()
			wait()
		end
	end
end)

uis.InputEnded:connect(function(inputObject)
	if inputObject.KeyCode == Enum.KeyCode.A then
		holdingA = false
	end
end)

6 comments

0

since you are waiting in the lerp fuction you do not need a wait on the while loop. you also should break out fo the loops when holdingA is false.

0

Your lerp function should be from start to end position. You use a.Position each time which changes store the start position in a variable and use that.

0

Thanks so much!

0

shouldn't lerp() be called from a.Position.CFrame?

0

just sayin'

0

lerp also works for Vector3s