I am trying to make a script that let’s an NPC walk toward a nearby player. I followed two tutorials by Thedevking for this. I tried to let the path update everytime the Position of the HumanoidRootPart changes, but it doesn’t work. The code works good without the Changed event, but with it, it doesn’t work. It doesn’t print Position Changed. Here is the code
local PS = game:GetService("PathfindingService")
local NPChum = script.Parent.Humanoid
local NPCRootPart = script.Parent.HumanoidRootPart
local function findTarget()
local agroDistance = 20
local target = nil
for i, v in pairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local humRootPart = v:FindFirstChild("HumanoidRootPart")
local NPC = v:FindFirstChild("NPC")
if human and not NPC then
if (NPCRootPart.Position - humRootPart.Position).magnitude < agroDistance then
agroDistance = (NPCRootPart.Position - humRootPart.Position).magnitude
target = humRootPart
end
end
end
return target
end
local humRootPart = nil
while wait(1) do
humRootPart = findTarget()
if humRootPart then
humRootPart.Changed:Connect(function(Position)
print("Position Changed")
local path = PS:CreatePath()
path:ComputeAsync(NPCRootPart.Position,humRootPart.Position)
local Waypoints = path:GetWaypoints()
for i, v in pairs(Waypoints) do
if Waypoints.Action == Enum.PathWaypointAction.Jump then
NPChum:ChangeState(Enum.HumanoidStateType.Jumping)
end
NPChum:MoveTo(humRootPart.Position)
NPChum.MoveToFinished:Wait(2)
end
NPChum:MoveTo(humRootPart.Position)
end)
end
end
It would be great if someone could help me with this
.Changed events are used for value objects such as IntValue and BoolValue. If you want to detect when a property of an object changes, you should use GetPropertyChangedSignal
Are you receiving any outputs? Maybe the code fails to continue from the “if humRootPart then”? which if it does, the problem will be distinguishable from there.
Changed fires if any property of a given Instance changes, so you are better off using :GetPropertyChangedSignal and then giving it the given property/value to detect the change on.
Also, I suggest making those lines a function after the position change handling.
local PS = game:GetService("PathfindingService")
local NPChum = script.Parent.Humanoid
local NPCRootPart = script.Parent.HumanoidRootPart
local function findTarget()
local agroDistance = 20
local target = nil
for i, v in pairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local humRootPart = v:FindFirstChild("HumanoidRootPart")
local NPC = v:FindFirstChild("NPC")
if human and not NPC then
if (NPCRootPart.Position - humRootPart.Position).magnitude < agroDistance then
agroDistance = (NPCRootPart.Position - humRootPart.Position).magnitude
target = humRootPart
end
end
end
return target
end
local function humRootPartChanged()
local path = PS:CreatePath()
path:ComputeAsync(NPCRootPart.Position,humRootPart.Position)
local Waypoints = path:GetWaypoints()
for _, v in pairs(Waypoints) do
if Waypoints.Action == Enum.PathWaypointAction.Jump then
NPChum:ChangeState(Enum.HumanoidStateType.Jumping)
end
NPChum:MoveTo(humRootPart.Position)
NPChum.MoveToFinished:Wait(2)
end
local humRootPart = nil
while wait(1) do
humRootPart = findTarget()
if humRootPart then
humRootPart:GetPropertyChangedSignal("Position"):Connect(function()
humRootPartChanged()
end)
end
end
Properties that update constantly like Position, CFrame, and Rotation don’t fire the Changed/GetPropertyChangedSignal events (unless manually set by a script). Besides that, it’s a bad idea to make new connections to an event every second without disconnecting them. Why don’t you just have the path generate once the target is found? It will still update every second, which should be plenty, but if it’s not you can use a faster loop with Humanoid:MoveTo() when the target is within a certain distance.
The Changed event fires the Position. I tested it out in a separate script with just a print and it worked.
I can’t generate a path once, because the map will have obstacles. I can remove the event and put it in a faster loop. I will probably do that if there is no solution to my problem. Thanks for the help