AI flying around and dying in void trying to walk to player

I’m trying to make this AI (SCP-106) come into the Pocket Dimension he sends you to and kill you if you don’t try to escape on your own. When he gets into the Pocket Dimension, he uses the same script to walk to you as he does at the beginning of the video.

For some reason, he flies around and off the map and then dies in the void instead of walking 2 feet to the person.

I’ve looked all over for a solution, but I just can’t figure it out.

-- code for the first part (that works)
local larm = script.Parent:FindFirstChild("HumanoidRootPart")
		local rarm = script.Parent:FindFirstChild("HumanoidRootPart")
		local scps = game.Workspace["SCP Entities"]

		local function findNearestTorso(pos)
			local list = game.Workspace:children()
			local torso = nil
			local dist = 10000
			local temp = nil
			local human = nil
			local temp2 = nil
			for x = 1, #list do
				temp2 = list[x]
				if (temp2.className == "Model") and (temp2 ~= script.Parent) then
					temp = temp2:findFirstChild("HumanoidRootPart")
					human = temp2:findFirstChild("Humanoid")
					if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
						if (temp.Position - pos).magnitude < dist then
							local pd = temp:FindFirstChild("PD")
							if temp.Parent.Parent ~= scps then
								if pd then
									if pd.Value == false then
										torso = temp
										dist = (temp.Position - pos).magnitude
									end
								end
							end
						end
					end
				end
			end
			return torso
		end
		while mode.Value == 2 do
			wait(1)
			local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
			if target ~= nil then
				script.Parent.Humanoid:MoveTo(target.Position, target)
			end
		end

-- code for the second part of the video (Where he has a stroke)

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
		local rarm = script.Parent:FindFirstChild("HumanoidRootPart")
		local scps = game.Workspace["SCP Entities"]

		local function findNearestTorso(pos)
			local list = game.Workspace:children()
			local torso = nil
			local dist = 10000
			local temp = nil
			local human = nil
			local temp2 = nil
			for x = 1, #list do
				temp2 = list[x]
				if (temp2.className == "Model") and (temp2 ~= script.Parent) then
					temp = temp2:findFirstChild("HumanoidRootPart")
					human = temp2:findFirstChild("Humanoid")
					if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
						if (temp.Position - pos).magnitude < dist then
							local pd = temp:FindFirstChild("PD")
							if temp.Parent.Parent ~= scps then
								if pd then
									if pd.Value == true then
										torso = temp
										dist = (temp.Position - pos).magnitude
									end
								end
							end
						end
					end
				end
			end
			return torso
		end
		while mode.Value == 4 do
			wait(1)
			local target = findNearestTorso(script.Parent:FindFirstChild("HumanoidRootPart").Position)
			if target ~= nil then
				script.Parent.Humanoid:MoveTo(target.Position, target)
			end
		end

What happens and how do I fix it? Here’s the video:

4 Likes

What does findNearestTorso return in the second part? One possible issue is that it is returning the wrong torso, causing it to travel to a completely different torso.

2 Likes

Already checked that by having it print the parent of the torso, and it returns my character.

It could be a collision issue with the room or both scripts are running the MoveTo command at the same time.

In the first half of the video, it does work fine. So the issue is likely to do with something in the second half. The main differences are the new environment (the room) and the script change.

Is the NPC in the first half and second half the same NPC or are they different with their own scripts? If the NPC and scripts are different, then it may suggest a room collision issue (since they are very similar)

Have you tried spawning the NPC in a different place for the second half? If not, you could try it. If it fixes the issue, then your room could be the issue.

You could also check if the humanoid WalkToPoint and WalkToPart properties are correct by printing. You could use the .Changed event to check this.

2 Likes

idk but a similar thing happened to with animations that required the NPC to teleport after the animation ended to it’s torso

you can’t fix the pivot error thing so basically you can test some theory i have, you basically insert a part via script anywhere on the center of the NPC either the head or the torso and set it to not collidable, don’t anchor it and make it invisible, massless and locked if you want, when you do the teleporting process you delete the part so the excess pivot rotation thing magiaclly disappears

Can’t be a collision issue, I looked at the NavMesh and the Decomp Geometry, both are fine. It can’t be both running at the same time because it’s all run in one script, and it’s impossible for both to run at the same time since each script runs off of an IntValue in the AI character, and can only run at a specific value.

I could try checking if it is due to an environment change, maybe because it’s under the baseplate? Not sure, I’ll have to check that out and get back to you.

It’s the same NPC with the same scripts that teleports to that second location.

I have not, I will try that also.

Will also check this.