For some reason, this door doesn't stop rotating at 90 degrees; it just keeps spinning and spinning. I think it has something to do with heartbeat, but what I was trying to do was make the door lerp smoothly using heartbeat, to the 90 degree mark. Here's a GIF of what it looks like:
https://gyazo.com/3633367499479c724ec662fe3ce17896
Here's the script:
local Can = true local Open = false script.Parent.Event.OnServerEvent:connect(function(Player) local Mag = (script.Parent.Center.Position-Player.Character.HumanoidRootPart.Position).magnitude if Mag <= script.Parent.Range.Value then if Can then Can = false if Open == false then game:GetService('RunService').Heartbeat:Connect(function(d) local finish = script.Parent.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0) local cfm = script.Parent.PrimaryPart.CFrame:lerp(finish,d*2) script.Parent:SetPrimaryPartCFrame(cfm) wait() end) Open = true else Open = false game:GetService('RunService').Heartbeat:Connect(function(d) local finish = script.Parent.PrimaryPart.CFrame*CFrame.Angles(0,-math.rad(90),0) local cfm = script.Parent.PrimaryPart.CFrame:lerp(finish,d*2) script.Parent:SetPrimaryPartCFrame(cfm) wait() end) end Can = true end end end)
Can someone tell me what's wrong? I'll fix it; but I just need to know what to fix first.
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.
local debounce = true local Open = false script.Parent.Event.OnServerEvent:Connect(function(Player) local Mag = (script.Parent.Center.Position-Player.Character.HumanoidRootPart.Position).magnitude if Mag <= script.Parent.Range.Value then if debounce then debounce = false if Open == false then local ellapedTime = 0 local finish = script.Parent.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(90),0) local connection -- this syntax is necessary connection = game:GetService('RunService').Heartbeat:Connect(function(lastBeat) ellapsedTime = ellapsedTime + lastBeat local d = ellapsedTime / TIME_TO_OPEN -- how long you want it to take to open local cfm = script.Parent.PrimaryPart.CFrame:lerp(finish, math.clamp(d, 0, 1)) script.Parent:SetPrimaryPartCFrame(cfm) if d >= 1 then connection:Disconnect() end end) Open = true else local ellapedTime = 0 local finish = script.Parent.PrimaryPart.CFrame * CFrame.Angles(0,-math.rad(90),0) local connection -- this syntax is necessary connection = game:GetService('RunService').Heartbeat:Connect(function(lastBeat) ellapsedTime = ellapsedTime + lastBeat local d = ellapsedTime / TIME_TO_CLOSE -- how long you want it to take to close local cfm = script.Parent.PrimaryPart.CFrame:lerp(finish, math.clamp(d, 0, 1)) script.Parent:SetPrimaryPartCFrame(cfm) if d >= 1 then connection:Disconnect() end end) Open = false end debounce = true end end end)