How to make an NPC go through a door

I was wondering if theirs a way to check if the npc is touching a door and then open the door but not let the player be able to do it that way since they use a prompt, and is there a way for the pathfinding to even ignore doors and find a pathway that goes through the door? thats alot to say but I would thank someone for helping me learn this!

door.Door.Touched:Connect(function(other)
	-- check if npc touches door then opens it 
	if debounce == true then
		debounce = false
		tweenOpen:Play()
		creakSFX:Play()
		prompt.ActionText = "Close"
		debounce = true
	end
end)
2 Likes

local door = script.Parent
local isOpen = false

function openDoor()
    if not isOpen then
        isOpen = true
        door.CFrame = door.CFrame * CFrame.new(0, 5, 0) -- Adjust the second value to how far up you want the door to move
    end
end

function closeDoor()
    if isOpen then
        isOpen = false
        door.CFrame = door.CFrame * CFrame.new(0, -5, 0) -- Adjust the second value to how far down you want the door to move
    end
end

door.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        openDoor()
        wait(3) -- Change this value to how long you want the door to stay open (in seconds)
        closeDoor()
    end
end)

2 Likes

this isnt what I wanted, I already made the script i need to figure out how I can check if an npc touches a door and if it does then it can open it. ONLY THE NPC not the player

1 Like

Nothing about some thing that I


local door = script.Parent -- Assumes the door is a direct child of the NPC model
local pathfindingService = game:GetService("PathfindingService")
local npc = script.Parent -- Assumes the script is a child of the NPC model

-- Define the destination for the NPC (in this case, the door)
local destination = door.Position

-- Calculate the path to the destination
local path = pathfindingService:CreatePath({
    AgentRadius = 2, -- Adjust this value based on the size of your NPC
    AgentHeight = 5, -- Adjust this value based on the height of your NPC
    AgentCanJump = true,
    AgentJumpHeight = 10, -- Adjust this value based on the jump capability of your NPC
    AgentMaxSlope = 45,
    })
path:ComputeAsync(npc.Position, destination)

-- Move the NPC along the path
path:MoveTo(destination)

-- Open the door (assuming the door has a script that opens it when touched)
door.Touched:Connect(function(hit)
    if hit.Parent == npc then
        -- Assume the door has a function called "Open" that opens the door
        door:Open()
    end
end)

1 Like

Never got your habit now in the script

1 Like

my head is broken cheeseman come in here

1 Like

check if the hit part has a humanoid inside of its parent and if its not a real player by checking if it doesnt exist in the Players container

door.Door.Touched:Connect(function(other)
	if (other.Parent:FindFirstChild("Humanoid")) and (game:GetService("Players"):FindFirstChild(other.Parent.Name) == nil)
		if debounce == true then
			debounce = false
			tweenOpen:Play()
			creakSFX:Play()
			prompt.ActionText = "Close"
			debounce = true
		end
	end
end)
2 Likes

Oops, maybe you got something wrong the head got broke

that sentence was the most clear ive heard from you

1 Like

thats an interesting way to do it I like it very much, thanks! I cant believe I didnt think of this.

3 Likes

338 solutions goodness how long you been scripting

2 Likes

since around late 2020 and im too lazy to make a game so i just go on the dev forum

Nice! Been doing it since 2022 but actually LEARNED it in 2023.

1 Like

An alternative would be creating two collision groups: one for the NPCs and another for the door. Only make the NPCs’ collision group noncollidable with the door using PhysicsService:CollisionGroupSetCollidable(NPC_COLLISION_GROUP_NAME, DOOR_COLLISION_GROUP_NAME, false) assuming that NPC_COLLISION_GROUP_NAME is a variable of the NPCs’ Collision Group’s name and DOOR_COLLISION_GROUP_NAME is a variable of the Door’s Collision Group’s name.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.