RunService and Lerp problem

Lerping is useful when you are changing the origin and the destination during runtime, as TweenService can’t do that

2 Likes

This is what I mean by using a loop

1 Like

RunService and looping are the same.
It is possible that you declared the original CFrame inside the loop’s scope, in which it is reset every new loop. You need to put it outside of the loop’s scope

1 Like

I’ve tried doing that and it doesn’t work somehow.

1 Like

Here’s the code that I tested

local part = script.Parent
local initialCFrame = part.CFrame
local clickDetector = part:WaitForChild("ClickDetector")

local targets = workspace:WaitForChild("Targets")
local target1 = targets:WaitForChild("1")

local connection1

connection1 = clickDetector.MouseClick:Connect(function(plr)
	
	while true do
		
		if part.CFrame == target1.CFrame then
			print("yes")
			break
		end
		
		task.wait(0.001)
		part.CFrame = initialCFrame:Lerp(target1.CFrame, 0.1)
	end
	
	connection1:Disconnect()
	print(connection1.Connected)
end)
1 Like

I’m sorry but you didn’t make a variable for the origin?
I’m guessing you’re doing that to simulate a EasingStyle but if you’re going for Linear then this wouldn’t work

part.CFrame = part.CFrame:Lerp(target1.CFrame, 0.1)
connection1 = clickDetector.MouseClick:Connect(function(plr)
	local originCF = part.CFrame
    local a = 0
	while a < 1 do
		
		task.wait()
        a += 0.1
		part.CFrame = originCF:Lerp(target1.CFrame, a)
	end
	
	connection1:Disconnect()
	print(connection1.Connected)
end)
2 Likes

I’ve changed it, the post is above

1 Like

I just changed your click detector event code, try to run my code and see it works. Also the loop inside your code wouldn’t stop because comparing 2 equal CFrames might not always be true as there’s an issue of floating point precision

3 Likes

Your code works, but it kind of passed the position of the target. So you know it’s not in the right spot

2 Likes

Anyway why did you put the original CFrame inside the connection? (it works)

1 Like

Because it is not within the loop’s scope
If you don’t know what scope is, I suggest you check out some articles on google or on youtube because I am not good at explaining things.

But in short, if you declare variables in the same code block as the loop, the variables are made anew every new loop, so you can’t save any data. Therefore I have to put the originalCF outside of the loop

2 Likes

No, I mean if I were to put the originalCF outside the function, it won’t work. Not the loop, so why is that?

1 Like

No, I mean if I were to put the originalCF outside the function, it won’t work. Not the loop, so why is that?

To be sure, did you want the click detector to only work once, but when you click it twice it does the same lerp?

1 Like

I mean, it should only just work once

1 Like

Here’s a video after testing your code

1 Like

If that’s the case, both my code and your code should work. However, since you were comparing CFrames to break out of the loop, the loop ends up never stop running. Therefore yielding the script and not executing the connection1:Disconnect() line
Because of that you can click again.

1 Like

Oh yeah it’s about the scope. Sorry, my bad.

1 Like

You can try printing a (alpha). I have some doubt it got to 1.1 before breaking out of the loop

2 Likes

This is very very weird, I don’t see anything wrong.

1 Like

Yep, it printed out 1.0999999999999999 at the end, right before it disconnects

1 Like