NPC Animation Script Suddenly Stopped Working

So, I have recently made an NPC system that I am using for spawning vehicles in my new game. More info on that can be found here and here.

I finally got around to porting the NPC system into my game, and it does not work as intended.

Here’s how it works as intended in the test place:
https://gyazo.com/4ca0223f89ccfef55f83cf993106dca7
https://gyazo.com/298e7b3f9c19be5d4dacf10a5049228d

Works pretty well, and works exactly as intended. Here’s the code for it:

local animation = script:WaitForChild("Wave",2)
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local rs = game:GetService("RunService")
local chars = {}

rs.Stepped:Connect(function(Time, deltaTime)
	for _,data in next,chars do
		if data.plrNear == true then
			local npcPosition = data.dummy.HumanoidRootPart.Position
			local playerPosition = char.HumanoidRootPart.Position
			local _,Y = CFrame.new(npcPosition,playerPosition):ToOrientation();
			data.dummy.HumanoidRootPart.CFrame = CFrame.Angles(0,Y,0)+npcPosition;
		end
	end
end)

for _,child in next,workspace:GetChildren() do
	if child.Name == "Dummy" then
		local data = {plrNear = false,dummy = child}
		chars[child] = data
		local prox = child.Head:WaitForChild("ProximityPrompt",3)
		if prox ~= nil then
			local playIt = child.Humanoid:LoadAnimation(animation)
			prox.PromptShown:Connect(function()
				chars[child].plrNear = true
				playIt:Play()
				playIt.Looped = true
				prox.PromptHidden:Wait()
				chars[child].plrNear = false
				playIt:Stop()
			end)
		end
	end
end

So tonight, I was porting this code over to my new game, and changed a few things. I changed the way the code detects if an NPC is in the game or not, since NPCs will get added (and removed) when players join/play the game, and their bases load/save. Very minor edits to the code:

local animation = script:WaitForChild("Wave",2)
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local rs = game:GetService("RunService")
local chars = {}


rs.Stepped:Connect(function(Time, deltaTime)
	for _,data in next,chars do
		if data.plrNear == true then
			local npcPosition = data.dummy.HumanoidRootPart.Position
			local playerPosition = char.HumanoidRootPart.Position
			local _,Y = CFrame.new(npcPosition,playerPosition):ToOrientation();
			data.dummy.HumanoidRootPart.CFrame = CFrame.Angles(0,Y,0)+npcPosition;
		end
	end
end)

game.Workspace.tycoons.DescendantAdded:Connect(function(child)
	if child.Name == "vehicleSpawnNPC" then
		task.wait(.5)
		local data = {plrNear = false,dummy = child}
		chars[child] = data
		local head = child:WaitForChild("Head",1)
		local prox = head:WaitForChild("ProximityPrompt",3)
		if prox ~= nil then
			local playIt = child.Humanoid:LoadAnimation(animation)
			prox.PromptShown:Connect(function()
				chars[child].plrNear = true
				playIt:Play()
				playIt.Looped = true
				prox.PromptHidden:Wait()
				chars[child].plrNear = false
				playIt:Stop()
			end)
		end
	end
end)

Yet, here is what happens to those same NPCs from before:
As you can see here, this NPC immediately faces away from my character, and does not follow my character around like in the test place.
The Second NPC does the same thing as the first here.

I’m not really sure what happened, or how to fix this. Is something about “DescendantAdded” causing this?

CFrame.lookAt.

You also need to make sure it’s the root motor, not the root CFrame.

changing it from CFrame.new to CFrame.lookAt did not fix the issue of it not facing me. Even if this was the issue, it was not happening in the test place.

Also, what does it mean with it being the root motor vs. root CFrame?

Another thing to note:
when I placed this system into the game that it was meant to be in, you can see that the NPC’s HumanoidRootPart’s orientation never changes, whereas in the test place, it was always changing

Very weird, but I fixed it. I had to move

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

To be within the RunService loop…

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