Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

My door keeps spinning, and won't stop at the 90 degree angle mark. What's wrong with it?

Asked by 5 years ago
Edited 5 years ago
1For 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:

01local Can = true
02local Open = false
03 
04script.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
07        if Can then
08            Can = false
09            if Open == false then
10                game:GetService('RunService').Heartbeat:Connect(function(d)
11                local finish = script.Parent.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0)
12                    local cfm = script.Parent.PrimaryPart.CFrame:lerp(finish,d*2)
13                    script.Parent:SetPrimaryPartCFrame(cfm)
14                    wait()
15                    end)
View all 29 lines...

Can someone tell me what's wrong? I'll fix it; but I just need to know what to fix first.

0
How long do you want the opening to last? compUcomp 417 — 5y
0
if it doesn't work, try using my model for help or something --> https://www.roblox.com/library/4559559581/Door NSMascot 113 — 5y

1 answer

Log in to vote
1
Answered by
compUcomp 417 Moderation Voter
5 years ago

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.

01local debounce = true
02local Open = false
03 
04script.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
07        if debounce then
08            debounce = false
09            if Open == false then
10                local ellapedTime = 0
11                local finish = script.Parent.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(90),0)
12                local connection -- this syntax is necessary
13                connection = game:GetService('RunService').Heartbeat:Connect(function(lastBeat)
14                    ellapsedTime = ellapsedTime + lastBeat
15                    local d = ellapsedTime / TIME_TO_OPEN -- how long you want it to take to open
View all 41 lines...
0
Wow! It's awesome that you fixed my script and everything, but there was one error. In my output, I got this: 18:47:44.198 - Workspace.Doors.Model.DoorMain:16: attempt to perform arithmetic on global 'ellapsedTime' (a nil value) 18:47:44.198 - Stack Begin 18:47:44.199 - Script 'Workspace.Doors.Model.DoorMain', Line 16 iinaoriku 52 — 5y
0
Nvm I solved the problem, but thanks for your help @compUcomp and @NSMascot iinaoriku 52 — 5y
Ad

Answer this question