NPC not fully moving to MoveTo part position

Hello! I am currently working on a game that involves the use of lots of NPCs. Currently, I am trying to get the NPC to walk out of their house during the day, and walk into the house during the night. However, the issue is that the NPC is not fully moving to the position (the pink part) outside the house, which is causing issues at least for when the NPC needs to go inside. Whenever the NPC doesn’t go to the exact position of the pink part during nighttime, the NPC can’t get through the House door.

Here is my script that makes the NPCs go outside the house during the day and go inside the house during the night.

local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local PathfindingService = game:GetService("PathfindingService")

local PlacedClassicHouses = game.Workspace.PlacedClassicHouses

PlacedClassicHouses.ChildAdded:Connect(function(child)
	if child:IsA("Model") then
		local NewHouse = child
		for i, v in pairs(NewHouse:GetChildren()) do
			if v:IsA("Model") and v.Name == "NPC" then
				local NPC = v
				if CollectionService:HasTag(NPC, "NPC") then
					local Humanoid = NPC.Humanoid
					
					local Move = NPC.Move -- script inside npc that makes them walk randomly
					local Humanoid = NPC.Humanoid
					local Torso = NPC.Torso
					local Goal1 = NewHouse.Goal1 -- inside house
					local Goal2 = NewHouse.Goal2 -- outside house
					
					local IsInside = true

					local function Sleep()
						
						local function GoToHouse()
							if IsInside == false then 
								Move.Enabled = false
								
								local path = game:GetService("PathfindingService"):CreatePath({
									AgentRadius = 1,
									AgentHeight = 0.2,
									AgentCanJump = false
								})
								path:ComputeAsync(Torso.Position, Goal2.Position)
								local waypoints = path:GetWaypoints()

								Torso:SetNetworkOwner(nil)

								local function NewPath()
									
									path:ComputeAsync(Torso.Position, Goal2.Position)
									local NewWaypoints = path:GetWaypoints()
									table.remove(NewWaypoints, 1)

									for i, waypoint in pairs(NewWaypoints) do
										Humanoid:MoveTo(waypoint.Position)
										Humanoid.MoveToFinished:Wait()
									end
								end
								
								local function OriginalPath()
									for i, waypoint in pairs(waypoints) do
										Humanoid:MoveTo(waypoint.Position)
										Humanoid.MoveToFinished:Wait()
									end
								end
								
								local head = NPC.Head
								local Length = 10

								local Ray = Ray.new(head.Position, head.CFrame.LookVector*Length)
								local Wall, HitPosition, Normal, Material = workspace:FindPartOnRay(Ray, NPC)
								if PlacedClassicHouses.ChildAdded then
									if Wall then
										NewPath()
									else if not Wall then
											OriginalPath()
										end
									end
								end
								
								IsInside = true
							end
						end
						
						local function GoOutside()
							if IsInside == true then
								Humanoid:MoveTo(Goal2.Position)
								Humanoid.MoveToFinished:Wait()
								wait(2)
								Move.Enabled = true
								IsInside = false
							end
						end
						RunService.Heartbeat:Connect(function()
							if game.Lighting.ClockTime > 18 or game.Lighting.ClockTime < 6 then
								GoToHouse()
								
								local distance = (Torso.Position - Goal2.Position).Magnitude
								
								while wait(distance) do
									Humanoid:MoveTo(Goal1.Position)
									Humanoid.MoveToFinished:Wait()
								end
								
							else
								GoOutside()
							end
						end)
					end
					Sleep()
				end
			end
		end
	end
end)

Here are some video examples of the issue:
This one is the NPC going outside.


This video is of the NPC traveling to the outside part to go inside.

TL;DR: NPC not fully moving to pink part position, causing issue when NPC needs to go inside house.

13 Likes

Instead of using MoveTo you should try using PathfindingService

7 Likes

Hi, thanks for the response! I thought that Pathfinding required MoveTo? At least thats what I did, I made a path and made the NPC MoveTo the waypoints. The Last waypoint should be the position of the pink part. Did I perhaps do the pathfinding wrong?

1 Like

Oh sorry its just that i didnt see the psthfinding service path of the script :sweat_smile:
The code seems fine to me maybe it has to do with Goal1 position, have you checked it?
And also the door pushes them when opening, something the pathfinding doesnt consider so maybe you could set the CanCollide property off of the door or updating the pathfinding

3 Likes

Oh that’s alright, I don’t blame you, it’s quite a long script just to move an NPC lol. Anyway, yeah I tried testing with completely removing anything that would make the NPC move to Goal1, so they should only move to goal2, yet they are still not fully moving to the right position. Any ideas?

2 Likes

They seem as if they are looking not to collide with the parts, maybe consider visualising the waypoints so it’s visible whether the issue is with the <Humanoid>:MoveTo() function or with <path>:GetWayPoints()

4 Likes

I have tested with making the door CanCollide false, but they will sometimes still get stuck on the wall. Should I just make the wall CanCollide false aswell?

2 Likes

Depends are you trying to achieve a realistic effect or just want them to go inside no matter what (sorry for the typos im on phone)

2 Likes

Also maybe their are getting stuck on the steps, try making them bigger so they cant get stuck on the walls

1 Like

Hello, I just tested what you said and made the waypoints visible as a glowing part, and I saw that the waypoint appears to be in the right position, but for some reason there seems to be a lot more waypoints that I would have thought would be needed as it appears like a line instead of dots/balls like it should be visualized as.

hjmgghf
Screenshot 2023-07-03 011030

3 Likes

Would you mind editing this message with a screenshot of what the waypoint visualisation looked like?

3 Likes

Hi, so I tried just removing the steps altogether to make sure the NPC wasn’t getting stuck, yet they still get stuck on the wall. I don’t want to have to make the walls CanCollide false unless thats the only way to get the NPC to go inside, but as of right now I’m trying to test with instead of using just MoveTo Goal1, I am making the NPC go another generated path to Goal1.

Also @theworldis_socruel I added the screenshots!

2 Likes

I just played around with pathfindingservice myself, everything seems to be working fine for me…
image
Any idea why your waypoints are lifted up in the air? Maybe it has to do something with that…?

3 Likes

Hi, so I set the waypoints in the air at first but I realize now I probably shouldn’t have done that lol. Anyway, have you tried testing the pathfinding with a smaller NPC? I think my issue is that the NPCs are too small. I’m going to test with everything at a normal size and see if there is any difference, I will edit this message when done testing.

edit: So, I was right. It has to do with the NPC’s size. I put the NPC at normal rig scale and the NPC moved to the correct position. I’m not sure what to do now though, any ideas? My game is supposed to be about building a tiny city with tiny NPCs, so I can’t keep them at this large scale.

2 Likes

I tried making the part fairly high, and you are right, it didn’t work. I also tried putting a wide obstacle that can’t be evaded, and it didn’t work until I enabled jumping, consider enabling jumping perhaps?

2 Likes

Hey so I reverted the NPC back to the small size and enabled jumping and it still doesn’t go to the full position, if thats what you were getting at. I’m honestly unsure of what to do.

3 Likes

What if you size down the parts/where the waypoints go (unless you already arranged that the waypoints are located on the floor/ground), would that work too (try a height that comes up to the knees of the character or lower)? I feel like relative size might be the cause here, in which case scaling up the character should have the same effect as scaling down the y coordinate of the target relative to the ground.

2 Likes

So I tried what you said, I changed the size of the pink part/where the path ends, and unfortunately still the NPC won’t go to the correct position. Maybe I try adding on to the position where the NPC should go, such as taking the position of the pink part then adding onto the X/Y/Z value(s)? Do you think that would work?

2 Likes

I’m not sure whether it would, but definitely do try it and let me know whether it works (and if it doesn’t, whether any behaviour of the system changes)

3 Likes

maybe consider making a normal sized rig and another rig but small
make the normal sized rig invisible and use it as a guide for small rig to teleport to and add animation?

5 Likes