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

How can I make my NPC open a door?

Asked by 4 years ago

So I am using alvin blox's piggy kit to make my own piggy game. Since the kit dosen't bot mode I decided to work on it on my own. But now I encountered a problem.

You see all the doors in game open on click by one module script and called the module from the main script.

So I decided to edit the script a little bit to make the door open automatically when the bot comes to the door.

My idea was to make a part that when touched will make the door open.

But for my unluck it is not working.

Original script by Alvin_Blox (This script makes the player open the door by clicking)

local module = {}

local tweenService = game:GetService("TweenService")

-- swing the door by `angle` degrees
function module.swingDoor(angle,door)
    -- locate the hinge on the door
    local hingeBefore = door.CFrame * CFrame.new(-2.5, 0, 0)

    -- relative to the hinge, where is the door?
    local relative = hingeBefore:toObjectSpace(door.CFrame)

    -- twist the hinge
    -- CONVERT angle *from* degrees *to* radians here
    local hingeAfter = hingeBefore * CFrame.Angles(0, math.rad(angle), 0)

    -- re-attach the door to the new "hinge"

    local tweenProperties = TweenInfo.new(0.2) -- Time
    local result = {CFrame = hingeAfter:toWorldSpace(relative)}

    door.CanCollide = false

    local tween = tweenService:Create(door,tweenProperties,result)
    tween:Play()

    wait(0.2)

    door.CanCollide = true
end

function module.ActivateDoors(doorFolder)
    for _, door in pairs(doorFolder:GetChildren()) do
        if door:FindFirstChild("Key") then

            door.Touched:Connect(function(hit)
                if hit.Parent:FindFirstChild("Key") then
                    if door.Key.Value == hit.Parent.Name then
                        if door.Name == "ExitDoor" then
                            if door.Generator.On.Value == true and not door:FindFirstChild("WoodBlock") then
                                door.Padlock.Anchored = false
                                wait(1)
                                door.Transparency = 1
                                door.CanCollide = false


                                door.Touched:Connect(function(hit)

                                    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

                                    if player then
                                        if player:FindFirstChild("Contestant") and not player:FindFirstChild("Escaped") then

                                            local Escaped = Instance.new("BoolValue")
                                            Escaped.Name = "Escaped"
                                            Escaped.Parent = player

                                            game.ReplicatedStorage.Announcement:FireClient(player,"You Escaped")

                                        end
                                    end


                                end)

                            end
                        else
                            door.Padlock.Anchored = false
                            wait(0.5)
                            door:Destroy()
                        end
                    end
                end
            end)

        else
            if not door:FindFirstChild("ClickDetector") then local cd = Instance.new("ClickDetector")

            cd.CursorIcon = "rbxassetid://5304494971"   
            cd.Parent = door 

            end

            local debounce = false

            door:FindFirstChild("ClickDetector").MouseClick:Connect(function(player)

                if door.Name == "Door" then
                    if not debounce then
                        debounce = true

                        local angle

                        if door:FindFirstChild("Open") then
                            angle = -90
                            door.Open:Destroy()     
                        else
                            angle = 90  
                            local openVal = Instance.new("StringValue")
                            openVal.Name = "Open"
                            openVal.Parent = door
                        end


                        module.swingDoor(angle,door)
                        wait(.001)


                        print("Door swung!")
                        wait(1)
                        debounce = false
                    end
                end
            end)
        end
    end
end

return module

The script that I made (or I edited alvin blox's script)

local module = {}

local tweenService = game:GetService("TweenService")

-- swing the door by `angle` degrees
function module.swingDoor(angle,door)
    -- locate the hinge on the door
    local hingeBefore = door.CFrame * CFrame.new(-2.5, 0, 0)

    -- relative to the hinge, where is the door?
    local relative = hingeBefore:toObjectSpace(door.CFrame)

    -- twist the hinge
    -- CONVERT angle *from* degrees *to* radians here
    local hingeAfter = hingeBefore * CFrame.Angles(0, math.rad(angle), 0)

    -- re-attach the door to the new "hinge"

    local tweenProperties = TweenInfo.new(0.2) -- Time
    local result = {CFrame = hingeAfter:toWorldSpace(relative)}

    door.CanCollide = false

    local tween = tweenService:Create(door,tweenProperties,result)
    tween:Play()

    wait(0.2)

    door.CanCollide = true
end

function module.ActivateDoors(doorFolder)
    for _, door in pairs(doorFolder:GetChildren()) do
        if door:FindFirstChild("Key") then

            door.Touched:Connect(function(hit)
                if hit.Parent:FindFirstChild("Key") then
                    if door.Key.Value == hit.Parent.Name then
                        if door.Name == "ExitDoor" then
                            if door.Generator.On.Value == true and not door:FindFirstChild("WoodBlock") then
                                door.Padlock.Anchored = false
                                wait(1)
                                door.Transparency = 1
                                door.CanCollide = false


                                door.Touched:Connect(function(hit)

                                    local player = game.Players:GetPlayerFromCharacter(hit.Parent)

                                    if player then
                                        if player:FindFirstChild("Contestant") and not player:FindFirstChild("Escaped") then

                                            local Escaped = Instance.new("BoolValue")
                                            Escaped.Name = "Escaped"
                                            Escaped.Parent = player

                                            game.ReplicatedStorage.Announcement:FireClient(player,"You Escaped")

                                        end
                                    end


                                end)

                            end
                        else
                            door.Padlock.Anchored = false
                            wait(0.5)
                            door:Destroy()
                        end
                    end
                end
            end)

        else
            if not door:FindFirstChild("ClickDetector") then 

            local cd = Instance.new("ClickDetector")
            cd.Parent = door 

            end

            local debounce = false

            workspace.DoorPart:Connect(function(hit)

                if hit.Parent.Name == "BotHarley" then

                if door.Name == "Door" then
                    if not debounce then
                        debounce = true

                        local angle

                        if door:FindFirstChild("Open") then
                            angle = -90
                            door.Open:Destroy()     
                        else
                            angle = 90  
                            local openVal = Instance.new("StringValue")
                            openVal.Name = "Open"
                            openVal.Parent = door
                        end


                        module.swingDoor(angle,door)
                        wait(.001)


                        print("Door swung!")
                        wait(1)
                        debounce = false
                    end
                end
                end 
            end)
        end
    end
end

return module

Then the function is called in the main script(Note:Whole script is not included)

local ninjadoor = require(script.NinjaModule)

if ClonedChap1:FindFirstChild("Doors") then 
    clickdoor.ActivateDoors(ClonedChap1.Doors)
else 
    warn("No Doors folder.")
end

1 answer

Log in to vote
0
Answered by 3 years ago

Try inserting this script in your NPC

local tweenService = game:GetService("TweenService")

-- swing the door by `angle` degrees
function swingDoor(angle,door)
    -- locate the hinge on the door
    local hingeBefore = door.CFrame * CFrame.new(-2.5, 0, 0)

    -- relative to the hinge, where is the door?
    local relative = hingeBefore:toObjectSpace(door.CFrame)

    -- twist the hinge
    -- CONVERT angle *from* degrees *to* radians here
    local hingeAfter = hingeBefore * CFrame.Angles(0, math.rad(angle), 0)

    -- re-attach the door to the new "hinge"

    local tweenProperties = TweenInfo.new(0.2) -- Time
    local result = {CFrame = hingeAfter:toWorldSpace(relative)}

    door.CanCollide = false

    local tween = tweenService:Create(door,tweenProperties,result)
    tween:Play()

    wait(0.2)

    door.CanCollide = true
end

script.Parent.HumanoidRootPart.Touched:Connect(function(hit)
    if hit.Parent.Name == "Door" then
        local door = hit.Parent
        local angle

        if door:FindFirstChild("Open") then
            angle = -90
            door.Open:Destroy()     
        else
            angle = 90  
            local openVal = Instance.new("StringValue")
            openVal.Name = "Open"
            openVal.Parent = door
        end


        swingDoor(angle,door)
        wait(.001)
    end
end)
Ad

Answer this question