I have been recently working on NPC behavior for my game, but I have encountered an issue. I have a script that detects the nearest player to an NPC and for optimization, the entire script is triggered using events and not “while task.wait() do” loops. However, one of my events, which is a GetPropertyChangedSignal event, only fires twice. Keep in mind, the GetPropertyChangedSignal event is contained within a for loop which is also contained within a PlayerAdded event.
it helped, but the issue is that the running event only triggers at the start of the movement and will only trigger again if the character changes direction or stops and moves again. this means that if the player just walks in a continuous straight line without changing direction, it can go undetected by the code. this is why i’d like to trigger every time the position changes.
I believe GetPropertyChangedSignal specifically does not work with Position and CFrame properties (likely performance reasons), unless those properties are set by some code. I think your easiest solution is to create a RunService loop and check when position changes occur.
local posChangeThreshold = 0.1 -- How far a character must displace to trigger position update.
local posLast = root.Position
game:GetService("RunService").Stepped:Connect(function()
if (posLast - root.Position).Magnitude >= posChangeThreshold then
posLast = root.Position
-- TODO position has changed, your code here.
end
end