Changed Event doesn't trigger

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

humRootPart:GetPropertyChangedSignal("Position"):Connect(function()
     print("Position changed to", humRootPart.Position)
end)

It still doesn’t work and are you sure that Changed Events are not for propertys, because it says in the object browser
Schermopname (2)

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.

I receive outputs from if humRootPart then. I don’t get any outputs after the .Changed event or after GetPropertyChangedSignal.

Some of the information here is wrong. Changed fires after any property of a given instance changes.

GetProperyChangedSignal fires when a GIVEN property of a given instance changes.

So with that being said, you can also use GetProperyChangedSignal on Int and Bool values.

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.

You can try this:

humRootPart:GetPropertyChangedSignal("Position"):Connect(function()

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
		
1 Like

It doesn’t work and it still doesn’t print “Position Changed”.

1 Like

Have you tried checking if the above if statement is met? Does it work without those events?

1 Like

The statement above is met and if I remove the event it works

1 Like

It may seem if the humRootPart is nil, can you try printing humRootPart to be sure it isn’t nil?

1 Like

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.

1 Like

humRootPart isn’t nil. I printed out humRootPart (printed HumanoidRootPart) and humRootPart.Parent (printed Bliksem18).

1 Like

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

1 Like