How to make an npc open doors?(No more solutions needed)

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) --- Please ignore this part.
	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") -- This is the part.
				
			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

I haven’t read it fully but, does your NPC have the “key”?

You see the key part has to be ignored.The player has to find the key as in piggy.

Kinda late but, you forgot to add the Touched event, you just put: workspace.DoorPart:Connect()

1 Like

:sweat_smile:
By the time you said this, I stopped coding and started to make art

2 Likes

Check if NPC is close to the door and then open it, use boolvalue so it wont break.

try this:

local npc = workspace."YourNPCName"

script.Parent.DoorFrame.Touched:Connect(function(hit)
local opened = false
if hit.Parent == npc.HumanoidRoorPart and opened == false then
local TweenService = game:GetService("TweenService")
local doorTween = TweenInfo.new(0.7,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out,0,false,0) <<<<< Put this if you want to make the door have a cinematic animation
local open = TweenService:Create(script.Parent.DoorFrame, doorTween, {CFrame = script.Parent.DoorOpen.CFrame})
opened = true
open:Play()
print(npc.Name, "opened the door!")
end
end)

this should work

1 Like

;-;

Thanks for replying but umm

1 Like