NPC colliding with part even though CanCollide is false

Basically, there is an NPC that wanders around the area in my game. This NPC, when it comes into contact with a door, will open it. However, the NPC collides with these doors, even though the doors have collisions off and collisions are only turned on for the client. Anyone know what to do about this?

My gosh this is like the 6th problem I’ve had with doors making this game… I’ve had less problems combined making the rest of the game! (4)

its print(“Waffles”) and not print “Waffles”. Do you come from Python?

Both work. You can even test it if you’d like. (BTW I have tried Python before a long time ago but it was a loooong time ago.)

Maybe the coroutine is causing the problem? Try removing it

I have removed it, but as stated in the note above it, it needs to be there so that the for loop can loop through all of the doors. My solution to this was to fire an event that activated another script containing the while loop, and in the end, it changed nothing.

nani

I never faced this kind of thing, so ig i cant help you/ gl

1 Like

You don’t need to remove the coroutine

Where did you even fire the coroutine, type coroutine.resume() function to fire a function the coroutine dosen’t work by itself you need to fire it and then pause the coroutine when needed

Check this: coroutine | Roblox Creator Documentation

coroutine.wrap() has always worked in such a way (for me at least) where it just automatically fires, and either way, that’s not the issue, as I stated previously that I tested it with firing a bindable event, and that didn’t work either.

Where’s the script that moves the NPC, and does it see the door as a blockage instead of being open?
You say the NPC ‘collides’ with a CanCollide off door, so I’d look into the NPC script rather than the door script.

The NPC does attempt to pathfind through the door, it doesn’t see the door as blockage. This is because, on the server, the door cannot be collided with. The way I prevent players from walking through the doors is by setting CanCollide to true on the client, meaning that it doesn’t effect the server. However, for some reason, the NPC still tries to pathfind through the door, but it collides with it for some reason.

Have you checked to see the difference between server and client in Studio test mode for the door’s CanCollide property?

Just checked, and CanCollide is set to false on the server, and true on the client. Also, “Waffles” was printed right when the map loaded. Not sure why.

EDIT: Waffles is printed when the map loads because I had the genius idea to put a print right at the start of the script for some reason. Wonderful.

Maybe change (script.Parent.HumanoidRootPart.Position - door.Position).magnitude <= 5 to (door.Position - script.Parent.HumanoidRootPart.Position).magnitude <= 5

That didn’t change anything. Still no printing, still no door opening.

I might look into RayCasts tomorrow, as after doing some more research, they might be able to help with the first problem.

Alright, the NPC is now opening doors.

This is the new code:

local chosenMap = game:GetService("ReplicatedStorage").Values.Chosen.ChosenMap
local rayOrigin = script.Parent.HumanoidRootPart

--Get all of the descendants of the current map
for _, door in pairs(game.Workspace.LoadedMaps:FindFirstChild(chosenMap.Value):GetDescendants()) do
	--Is it a door?
	if door:FindFirstChild("IsOpen") then
		--Coroutine so that the for loop can continue and not get stuck on the while loop
		coroutine.wrap(function()
			while wait() do
				--Check if near door & door is unlocked and closed
				if Ray.new(rayOrigin.Position, door.Position - rayOrigin.Position):Distance(door.Position) <= 5 and not door.IsOpen.Value and not door.IsOpening.Value and (not door:FindFirstChild("Unlocked") or door:FindFirstChild("Unlocked").Value) then
					print "Waffles"
					script.Parent.Humanoid.WalkSpeed = 0
					wait(1)
					script.Parent.Humanoid.WalkSpeed = 10
					--Open the door
					require(game.ServerScriptService.Scripts.Functions.DoorFunctionStorage).DoorOpenClose(game:GetService("RunService"), door.Parent, door.Parent:GetPivot(), not door:FindFirstChild("IsOpen").Value, door:FindFirstChild("IsOpen").Value, door.Orientation.Y, false)
				end
			end
		end)()
	end
end

I’ve still got the problem of the NPC colliding with the door, though.

Since doors are turned on for the client, if the NPCs RootPart network ownership is set to the client / player, it will collide with the door. Try setting the network ownership to nil.

1 Like

For some reason, I get an error that says that I can’t call :SetNetworkOwner on an anchored part or a part welded to an anchored part, even though not a single part in the NPC is anchored.

Okay what the error is gone now…

Alright, that worked! Thanks for the help!