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

how to make a open close door like lumber tycoon 2 ?

Asked by 6 years ago
Edited 6 years ago

im having a problem its not working i cant find out why pls i need help

local runService = game:GetService("RunService") local createButton = require(game.ServerScriptService.GameObjects.WireItems.CreateButton)

local doorModule = {}

function doorModule.new(doorList)

for _, door in pairs(doorList) do
    local hingeAnchor = door.Anchor
    local hinge = door.Hinge

    local pos = door.Pos
    local posConstraint = door.PosConstraint

    local toggleMotor = Instance.new("Motor")
    toggleMotor.Part0 = hingeAnchor
    toggleMotor.Part1 = hinge
    toggleMotor.C0 = CFrame.Angles(-math.pi/2, math.pi, 0) * CFrame.new(hingeAnchor.Size.X / 2, 0, 0)
    toggleMotor.C1 = CFrame.Angles(0, math.pi/2, 0) * CFrame.new(hinge.Size.Z / 2, 0, 0)
    toggleMotor.MaxVelocity = 0.08
    toggleMotor.Parent = toggleMotor.Part0

    door.Model:MakeJoints()

    local function unanchor(part)
        for _, part in pairs(part:GetConnectedParts()) do
            if part.Anchored and not (part == hingeAnchor) and part.Parent == door.Model then
                part.Anchored = false
                unanchor(part)
            end
        end
    end
    unanchor(hinge)

    if not door.Remote:FindFirstChild("ButtonPrompt") then
        print(door.Remote:GetFullName())
    end
    local doorName = door.Remote.ButtonPrompt.Value

    local function toggleSwitch()

        toggleMotor.DesiredAngle = pos.Value * math.pi/2

        if pos.Value == 0 then
            door.Remote.ButtonPrompt.Value = "Open "..string.lower(doorName)
        else
            door.Remote.ButtonPrompt.Value = "Close "..string.lower(doorName)
        end
    end


    createButton:NewPulse(door.Remote, function(player, wire)

        local torso
        if not wire then
            torso = player.Character and player.Character:FindFirstChild("Torso")
        elseif wire then
            torso = wire.PrimaryPart
        end         

        if not (pos.Value == 0) or not torso then
            pos.Value = 0
        else
            if posConstraint then
                pos.Value = posConstraint
            else
                local p = hingeAnchor.CFrame:pointToObjectSpace(torso.Position)
                if p.Z > 0 then
                    pos.Value = 1
                else
                    pos.Value = -1
                end 
            end
        end

        toggleSwitch()
    end)

    pos.Changed:connect(toggleSwitch)
    toggleSwitch()

    local seat = door.VehicleSeat
    if seat then
        seat.Changed:connect(function(property)
            if property == "Occupant" then
                if seat.Occupant then
                    pos.Value = 0
                else
                    pos.Value = posConstraint or 1
                end                 
                toggleSwitch()
            end
        end)
    end
end

end

return doorModule

Answer this question