Lerp stands for Linear intERPolation. CFrame:lerp(new, d)
is defined as a new CFrame that is d
of the distance between the current CFrame
and new
. d
is usually between 0 and 1 but it can be larger to go past new
. Heartbeat
event passes the time since the last Heartbeat
in seconds. It does not give you the time since you connected your listener to the event. In addition, finish
is constantly being recalculated to be 90 degrees ahead of where the door is now. I've fixed all of these problems here.
04 | script.Parent.Event.OnServerEvent:Connect( function (Player) |
05 | local Mag = (script.Parent.Center.Position-Player.Character.HumanoidRootPart.Position).magnitude |
06 | if Mag < = script.Parent.Range.Value then |
11 | local finish = script.Parent.PrimaryPart.CFrame * CFrame.Angles( 0 ,math.rad( 90 ), 0 ) |
13 | connection = game:GetService( 'RunService' ).Heartbeat:Connect( function (lastBeat) |
14 | ellapsedTime = ellapsedTime + lastBeat |
15 | local d = ellapsedTime / TIME_TO_OPEN |
16 | local cfm = script.Parent.PrimaryPart.CFrame:lerp(finish, math.clamp(d, 0 , 1 )) |
17 | script.Parent:SetPrimaryPartCFrame(cfm) |
19 | connection:Disconnect() |
25 | local finish = script.Parent.PrimaryPart.CFrame * CFrame.Angles( 0 ,-math.rad( 90 ), 0 ) |
27 | connection = game:GetService( 'RunService' ).Heartbeat:Connect( function (lastBeat) |
28 | ellapsedTime = ellapsedTime + lastBeat |
29 | local d = ellapsedTime / TIME_TO_CLOSE |
30 | local cfm = script.Parent.PrimaryPart.CFrame:lerp(finish, math.clamp(d, 0 , 1 )) |
31 | script.Parent:SetPrimaryPartCFrame(cfm) |
33 | connection:Disconnect() |