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?