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

CFrame:lerp() Not Working. Any help?

I have some code in a local script here but it is not working how it should. Its not even changing the CFrame at all and there are no errors in the output. Any ideas? Thanks.

while true do
local NewCFrame = CFrame.new(script.Parent.Parent.Torso.CFrame.X, script.Parent.Parent.Torso.CFrame.Y + 3.5, script.Parent.Parent.Torso.CFrame.Z + 3)
script.Parent.CFrame:lerp(NewCFrame, 0.5)
wait(0.5)
end

1 answer

✓ Accepted answer

Here's a fixed version of your script:

while true do
    local newCFrame = CFrame.new(script.Parent.Parent.Torso.CFrame.p + Vector3.new(0, 3.5, 3))
    local parentCFrame = script.Parent.CFrame
    
    script.Parent.CFrame = parentCFrame:lerp(NewCFrame, 0.5)
    
    wait(0.5)
end

Here's what I changed:

Firstly, I changed this line of code

local NewCFrame = CFrame.new(script.Parent.Parent.Torso.CFrame.X, script.Parent.Parent.Torso.CFrame.Y + 3.5, script.Parent.Parent.Torso.CFrame.Z + 3)

into this:

local newCFrame = CFrame.new(script.Parent.Parent.Torso.CFrame.p + Vector3.new(0, 3.5, 3))

Rather than creating a CFrame with just raw numbers, you can also create one with a Vector3. So, the new operation does some Vector3 math and then creates a CFrame from the result.

Finally, you had this line of code:

script.Parent.CFrame:lerp(NewCFrame, 0.5)

Which was changed to

script.Parent.CFrame = parentCFrame:lerp(NewCFrame, 0.5)

The reason the old one wasn't working is because lerp is a function of CFrame. Just using lerp doesn't move an object, it returns a CFrame lerped between two CFrames.

That's why you also need to set the CFrame of the object you want to move to what you calculate with lerp.

I hope this helps. Good luck!

1 comment

0

Thanks :))) (I'm not to good at moving stuff and CFrame. I mostly stick to GUIs and stuff)

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