Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can I make a door only CanCollide for NPC?

I have the code here basically the NPC walks out the house and back in after doorbell is rang. I want it to where only the NPC can go through the door while the Player cannot.

local NPC = script.Parent.Parent.H1.NPC.Humanoid
local NPCMod = script.Parent.Parent.H1['NPC'].Torso
local pos1 = script.Parent.Parent.H1.inPart.Position
local pos2 = script.Parent.Parent.H1.outPart.Position
local door = script.Parent.Parent.H1.Door

deb = true 
script.Parent.Clicker.ClickDetector.MouseClick:connect(function(wat)
	if deb == true then
		deb = false
			-- NPC moves outside when doorbell clicked
			door.CanCollide = false -- I do not like this because the Player can go through the door too!
			NPC:MoveTo(pos2)
			wait(.25)
			NPCMod.Anchored = true
			door.CanCollide = true
			wait(5)
			NPCMod.Anchored = false

			-- NPC moves inside after 5 seconds
			door.CanCollide = false
			NPC:MoveTo(pos1)
			wait(.27)
			NPCMod.Anchored = true
			door.CanCollide = true
			deb = true
	end
end)

6 comments

0

Uh, this is half-assed but, you might be able to use FE to your advantage here.

0

I'm a pretty new scripter so I mean this is all I would know how to do for now... What does FE stand for?

0

FE = Filtering Enabled

1

Add the door from a client's localscript.Thus,the door is visible for the player,but not to the NPC.

0

I like that idea, and it would probably work the best. ^

0

Thanks for the tips guys I'll try it out! I never thought about making the doors for the client..

1 answer

You could honestly just probably check anything that only exists in the NPC and check to see if whatever touches the door has it present. So, like, if the NPC's model name is "Jeff," then check to see if whatever touched the door is named "Jeff," and if not, don't open it (unless, of course, if someone on ROBLOX is simply named "Jeff" O_O). Example:

name = "Jeff";
debounce = false;

script.Parent.Touched:connect(function(hit)
    if not debounce then
        debounce = true;

        local thingThatTouchedTheDoor = hit.Parent;
        
        if thingThatTouchedTheDoor.Name == name then
            script.Parent.CanCollide = false;
            wait(0.5) -- maybe a bit longer, depending on how fast the npc is able to make it through the door >_>
            script.Parent.CanCollide = true;
        end
        
        debounce = false;
    end
end)

There's definitely a more efficient way to accomplish what you seek, but this is just off the top of my head.

This discussion is preserved as it was. New answers, comments, and votes are closed.