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
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.

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.

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)
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