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

Help with :Lerp() please?

I am trying to learn how to do lerps since it's similar to doing Tweening with and I need to make animations without the plugin so I need to do Lerp and I read the wiki about it however, I don't understand the way the Roblox wiki explained it I tried some of their methods I don't get how to do it but, don't worry I still gave it a shot here is my 'shot'... This might get ugly(If it isn't already) I NEED TO MAKE THE PART SMOOTHLY TRANSITION TO THE OTHER PART! LERP PRACTICE

script.Parent.Touched:connect(function(part)
	print("Lerping")
	local humanoid = part.Parent:WaitForChild("Humanoid")
	if humanoid then
          script.Parent.CFrame:lerp(script.Parent.Parent.ApproachingPart.CFrame, .5) --[[ 
			ApproachingPart is in the workspace and so is the Part this script is under.
		--]]
	end
end)

Thank you for reading all the way I am sorry if this was ugly for you to watch.... But I need help and badly. So I hope you can help me out it would be greatly appreciated!

1 answer

✓ Accepted answer

It looks like you want the brick to teleport halfway between its current CFrame and ApproachingPart's CFrame when a player touches it.

All that Start:Lerp(Destination, Percent) does is return a CFrame that is linearly interpolated between two points. Start is the CFrame you start with, Destination is the CFrame you want to end with, and Percent describes how much you want to tween in (e.g. setting Percent to 0.50 will return a CFrame that is halfway (50%) between Start and Destination. Setting Percent to 1.00 will effectively return Destination (100%)).

In your script, all you forgot to do is make the CFrame of the part equal to the value to got from :Lerp.

script.Parent.Touched:connect(function(part)
    print("Lerping")
    local humanoid = part.Parent:WaitForChild("Humanoid")
    if humanoid then
          script.Parent.CFrame = script.Parent.CFrame:lerp(script.Parent.Parent.ApproachingPart.CFrame, .5) -- set the parts CFrame to what you got from :Lerp()
    end
end)

However, this is just one way of using :Lerp. What :Lerp is really useful for is making smooth transitions between two CFrames. To make the part smoothly transition between its current position and ApproachingPart's position, do this:

-- it is very good practice to store variables when you expect to use them frequently.
-- it's also less clutter on the script.
local startpart = script.Parent
local approachingpart = game.Workspace.ApproachingPart

-- the CFrame the part started with
local startcf = startpart.CFrame
-- the CFrame you want the part to end with
local endcf = approachingpart.CFrame

startpart.Touched:connect(function(part)
	local humanoid = part.Parent:WaitForChild("Humanoid")
	if humanoid then
		print("Lerping") -- this should probably be inside the if-statement
		for per = 0.00, 1.00, wait() do -- from 0% to 100% of the way, do...
			startpart.CFrame = startcf:lerp(endcf, per)
			wait()
		end
	end
end)

10 comments

1

This script wouldn't work due to Lua sensitivity on line 16, ':lerp' not ':Lerp'.

0

ffixed

0

Is there anyway I could use this to deal with animations? I mean :Lerp() If so can you tell me how I would do that? How would I get the specific coordinates I want the part to reach and all that...

0

No I don't want the part to 'teleport' to the other part...

0

I want that part to smoothly transition to the other part... Is that possible? If so please can you add into your answer how?

0

Look at the second script I posted...

1

just add a 'wait()' to the for loop.

0

Huh, I though the wait() embedded in the for loop would work. Can someone test this?

1

Never mind I learned how to transition it was rather simple xD I just added increments basically.... I made the wait go on the next line and made increments of .005 and I think that's really cool. Thanks for helping me out!

0

Just tested it. Turns out the wait embedded in the for-loop doesn't actually yield the thread :s

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